How-to articles, tricks, and solutions about PYTHON

How to make IPython notebook matplotlib plot inline

To make matplotlib plots show up inline in an IPython notebook, you can use the following code snippet:

"inconsistent use of tabs and spaces in indentation"

"Inkonsistente Verwendung von Tabs und Leerzeichen in Einrückung" is an error message that you may encounter when working with Python code.

Which version of Python do I have installed?

You can find the version of Python that you have installed by running the following command in your command prompt or terminal:

How to set the y-axis limit

In matplotlib, you can set the limit of the y-axis of a plot by using the set_ylim() method of the Axes class.

Convert pandas dataframe to NumPy array

In pandas, you can convert a DataFrame to a NumPy array by using the values attribute.

Changing the tick frequency on the x or y axis

In matplotlib, you can change the tick frequency on the x or y axis of a plot by using the set_xticks() or set_yticks() method of the Axes class.

Set value for particular cell in pandas DataFrame using index

In pandas, you can set the value of a specific cell in a DataFrame using the at method.

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

In pandas, you can use the groupby() method to group data by one or more columns and then use the agg() method to compute various statistics for each group.

Convert all strings in a list to int

In Python, you can use the map() function along with the built-in int() function to convert all strings in a list to integers.

How to disable Python warnings?

There are a few different ways to disable warnings in Python, depending on the specific warning and the scope in which you want to suppress it.

Styling multi-line conditions in 'if' statements?

In Python, there are a few different ways to style multi-line conditions in if statements, depending on the complexity of the condition and personal preference.

How to import the class within the same directory or sub directory?

In Python, you can use the import statement to import a module or class from a file within the same directory or a subdirectory.

Print string to text file

In Python, you can use the built-in open() function to create a text file and write a string to it.

Convert hex string to integer in Python

You can use the int() function in Python to convert a hex string to an integer.

Is there a list of Pytz Timezones?

Yes, you can use the all_timezones attribute of the pytz library to get a list of all the timezones that it supports.

Running unittest with typical test directory structure

Here is an example of how you can run unittests in Python using the typical test directory structure:

How do I convert a datetime to date?

You can use the date() function from the datetime module in Python to convert a datetime object to just a date object.

Automatically create requirements.txt

You can use the pip freeze command to automatically generate a requirements.txt file in Python.

Python `if x is not None` or `if not x is None`?

The proper way to check if a variable x is not None in Python is to use if x is not None.

What do __init__ and self do in Python?

__init__ is a special method in Python classes, also known as a constructor.

Reverse / invert a dictionary mapping

Here's an example of how you can reverse or invert a dictionary mapping in Python:

Why can't Python parse this JSON data?

Without a specific code snippet and the JSON data that is causing issues, it is difficult to determine why Python is unable to parse the data.

How do I check the versions of Python modules?

You can use the built-in "pkg_resources" module to check the version of a Python module.

Python integer incrementing with ++

Python does not have a ++ operator for incrementing integers like some other programming languages.