How-to articles, tricks, and solutions about PYTHON

not None test in Python

In Python, you can check if a variable is not equal to None using the is not operator.

Null object in Python

In Python, the "null" object is called None.

open() in Python does not create a file if it doesn't exist

Here is a code snippet that demonstrates how to use the open() function in Python to create a new file if it does not already exist:

Pandas DataFrame Groupby two columns and get counts

Here is an example code snippet that demonstrates how to use the groupby() method in pandas to group a DataFrame by two columns and get the counts for each group:

Pandas index column title or name

To set the name of the index column in a pandas DataFrame, you can use the .rename_axis() method or the .index.name attribute.

Pandas Merging 101

Here is an example of how to use the pd.merge() function to merge two DataFrames in pandas:

Parsing boolean values with argparse

Here is an example of how to parse boolean values with argparse in Python:

Peak detection in a 2D array

Here is an example of a Python function that can be used to detect peaks in a 2D array:

pg_config executable not found

Check the location of pg_config: Open a terminal and run the command "which pg_config".

pip install fails with "connection error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:598)"

This error message occurs when pip is unable to verify the certificate of the website it is trying to connect to.

pip install from git repo branch

You can use pip to install a package from a git repository and specify a branch by using the following syntax:

pip install mysql-python fails with EnvironmentError: mysql_config not found

This error occurs when the mysql-config command is not in the system's PATH.

pip uses incorrect cached package version, instead of the user-specified version

Here is an example of how to use pip to install a specific version of a package, rather than using a cached version:

Plot logarithmic axes

Here is a code snippet that shows how to plot a graph with logarithmic scales for both the x and y axes in Python using the Matplotlib library:

Pretty-print an entire Pandas Series / DataFrame

You can use the .head() method to print the first few rows of a Pandas Series or DataFrame in a "pretty" format.

Print multiple arguments in Python

Here is an example of how to print multiple arguments in Python:

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.

Printing Lists as Tabular Data

Here is an example of how to print a list of lists (a 2D list) as tabular data using Python:

Proper way to declare custom exceptions in modern Python?

To declare a custom exception in Python, you can create a new class that inherits from the built-in Exception class.

Purpose of "%matplotlib inline"

The purpose of "%matplotlib inline" in Python is to display matplotlib plots within the Jupyter notebook.

Putting a simple if-then-else statement on one line

In Python, you can put a simple if-then-else statement on one line using the ternary operator, which is represented by the "?" symbol.

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.

Python - Count elements in list

You can use the len() function to count the number of elements in a list.

Python - TypeError: 'int' object is not iterable

This error message is indicating that you are trying to iterate over an object of type 'int', which is not iterable (i.e.

Python 3: UnboundLocalError: local variable referenced before assignment

This error occurs when you are trying to access a variable before it has been assigned a value.