How-to articles, tricks, and solutions about PYTHON

Get a list from Pandas DataFrame column headers

You can use the DataFrame.columns attribute to access the column labels of a DataFrame as an Index object.

Create a Pandas Dataframe by appending one row at a time

Here is an example of creating a Pandas DataFrame by appending one row at a time:

pg_config executable not found

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

Correct way to write line to file?

The correct way to write a line to a file in Python is to use the write() method on the file object, followed by the line you want to write.

Display number with leading zeros

You can use the str.format() method to display a number with leading zeros in Python.

How do I create a constant in Python?

In Python, a constant is typically defined as a variable that is assigned a value that should not be modified later on in the program.

Getting key with maximum value in dictionary?

You can use the built-in max() function in Python to get the key with the maximum value in a dictionary.

How can I do a line break (line continuation) in Python?

In Python, you can use the "" character to indicate a line continuation.

Running shell command and capturing the output

In Python, you can use the subprocess module to run shell commands and capture their output.

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.

Get list from pandas dataframe column or row?

In Pandas, a DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

Python Pandas: Get index of rows where column matches certain value

You can use the .loc method to filter the DataFrame and get the boolean mask, and then use the .index property to get the index of the rows that match the certain value.

How can I print variable and string on same line in Python?

You can use the print() function in Python and use the + operator to concatenate a string and a variable.

Maximum and Minimum values for ints

In most modern programming languages, the maximum value for an int data type is implementation-specific, but is typically in the range of 2^31 - 1 to 2^63 - 1, and the minimum value is usually -2^31 or -2^63.

How to emulate a do-while loop?

In Python, you can emulate a do-while loop by using a while True loop with a break statement.

What IDE to use for Python?

There are many Integrated Development Environments (IDEs) that you can use for writing, testing, and debugging Python code.

Deleting DataFrame row in Pandas based on column value

In Pandas, you can delete a row in a DataFrame based on a certain column value by using the drop() method and passing the index label of the row you want to delete.

How to check if a string is a substring of items in a list of strings

You can use a for loop to iterate through the list of strings, and then use the in keyword to check if the string is a substring of the current item in the list.

How do I call a function from another .py file?

You can call a function from another .py file by importing the file and then calling the function.

Is arr.__len__() the preferred way to get the length of an array in Python?

No, in python the built-in len() function is the preferred way to get the length of an array, list or any other iterable object.

How to round to 2 decimals with Python?

You can use the built-in round() function to round a decimal to a specific number of decimal places in Python.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The error message "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" typically occurs when you are trying to parse a string as JSON, but the string is not in a valid JSON format.

How do I create a list with numbers between two values?

Here is an example of how you might create a list of numbers between two values in python:

How do I convert all strings in a list of lists to integers?

Here is an example of how you might convert all strings in a list of lists to integers:

How do I list all files of a directory?

In Python, you can use the os module to list all files in a directory.