How-to articles, tricks, and solutions about PYTHON

How to search and replace text in a file?

There are several ways to search and replace text in a file, depending on the programming language you are using.

What is the maximum recursion depth in Python, and how to increase it?

The maximum recursion depth in Python is typically 1000, although this can vary depending on the operating system and system settings.

Why does code like `str = str(...)` cause a TypeError, but only the second time?

Code like str = str(...) causes a TypeError because it attempts to re-assign the built-in str type to a new value, which is not allowed.

How to clear the interpreter console?

There are a few ways to clear the interpreter console in Python, depending on the specific environment you are using.

Reading JSON from a file

Here is an example of reading a JSON file in Python:

How to add an empty column to a dataframe?

In pandas, you can add an empty column to a DataFrame using the assign() method or the insert() method.

What is the quickest way to HTTP GET in Python?

Here's a code snippet for making an HTTP GET request using the requests library in Python:

How to normalize a NumPy array to a unit vector?

To normalize a NumPy array to a unit vector, you can use the numpy.linalg.norm function to calculate the magnitude of the vector, and then divide the array by this magnitude.

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.

How do I find out my PYTHONPATH using Python?

You can use the sys module in Python to access the sys.path variable, which contains a list of directories that Python looks in for modules to import.

Remove all special characters, punctuation and spaces from string

Here is an example of code that removes all special characters, punctuation, and spaces from a string in Python:

Convert list to tuple in Python

You can convert a list to a tuple in Python by using the built-in tuple() function.

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:

Finding local IP addresses using Python's stdlib

Here is a code snippet that uses the socket module from Python's standard library to find the local IP addresses of the host machine:

Alphabet range in Python

In Python, you can use the string module's ascii_lowercase and ascii_uppercase constants to get the range of lowercase and uppercase letters in the ASCII character set respectively.

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:

python numpy ValueError: operands could not be broadcast together with shapes

The ValueError: operands could not be broadcast together with shapes error occurs in NumPy when the shapes of the arrays being operated on are incompatible.

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:

How to urlencode a querystring in Python?

You can use the urllib.parse.urlencode() function to urlencode a querystring in Python.

Matplotlib make tick labels font size smaller

Here is a code snippet that shows how to make the tick labels font size smaller in Matplotlib:

Converting a Pandas GroupBy output from Series to DataFrame

Here is an example code snippet that demonstrates how to convert the output of a Pandas GroupBy operation from a Series to a DataFrame:

Unicode (UTF-8) reading and writing to files in Python

To read a file in Unicode (UTF-8) encoding in Python, you can use the built-in open() function, specifying the encoding as "utf-8".

How to set the current working directory?

You can set the current working directory in Python using the os module, specifically the chdir() function.

1 2 3 4 5