How-to articles, tricks, and solutions about PYTHON

How do I split a string into a list of characters?

In Python, you can use the built-in list() function to convert a string into a list of characters.

How do I access the ith column of a NumPy multidimensional array?

You can access the ith column of a NumPy multidimensional array by using the following syntax:

Split string on whitespace in Python

You can use the split() method to split a string on whitespace in Python.

How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

You can replace NaN values in a column of a Pandas Dataframe by using the fillna() method and passing in the value you want to replace NaN with.

How do I read CSV data into a record array in NumPy?

You can use the numpy.genfromtxt() function to read CSV data into a NumPy record array.

Convert a Unicode string to a string in Python (containing extra symbols)

You can use the .encode() method to convert a Unicode string to a string containing extra symbols in Python.

Python string.replace regular expression

You can use the re module in Python to use regular expressions with the replace() method.

Changing one character in a string

Here is a code snippet that demonstrates how to change a single character in a string in Python:

How to extract the substring between two markers?

In Python, you can use the str.find() method to find the index of the first marker, and the str.rfind() method to find the index of the second marker.

Get first row value of a given column

Here's an example of how you can get the first row value of a given column in a Pandas DataFrame in Python:

Count the frequency that a value occurs in a dataframe column

Here is an example code snippet that counts the frequency of values in a column of a Pandas DataFrame:

How to write inline if statement for print?

Inline if statements, also known as ternary operators, can be used to write a shorthand version of an if-else statement.

How to change the figure size of a seaborn axes or figure level plot

You can change the figure size of a Seaborn plot by using the set_size_inches() method of the matplotlib.pyplot.figure object and passing in the desired width and height.

Adding a legend to PyPlot in Matplotlib in the simplest manner possible

Here is a simple code snippet that demonstrates how to add a legend to a PyPlot in Matplotlib:

Reading binary file and looping over each byte

Here is an example code snippet that demonstrates how to read a binary file and loop over each byte in Python:

How to uninstall Python 2.7 on a Mac OS X 10.6.4?

To uninstall Python 2.7 on a Mac OS X 10.6.4, you can use the following commands in the terminal:

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

To create a Pandas DataFrame from a Numpy array and specify the index column and column headers, you can use the pd.DataFrame() constructor and pass in the Numpy array, as well as the index, columns parameters.

Writing string to a file on a new line every time

Here is an example code snippet for writing a string to a file on a new line every time in Python:

How to install PIL with pip on Mac OS?

To install PIL (Python Imaging Library) using pip on Mac OS, you can use the following command in your terminal:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c

This error occurs when a file or string that is being decoded using the UTF-8 encoding contains an invalid byte sequence.

Print multiple arguments in Python

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

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.

ImportError: No module named PIL

Here is a code snippet that demonstrates how to catch the "ImportError: No module named PIL" error:

ImportError: No module named pip

Here is a code snippet that demonstrates how to handle the "No module named pip" error:

Python - Count elements in list

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