How-to articles, tricks, and solutions about PYTHON

How to apply a function to two columns of Pandas dataframe

To apply a function to two columns of a Pandas DataFrame, you can use the apply() method of the DataFrame and pass the function as an argument.

How can I use threading in Python?

Threading is a way to run multiple threads (smaller units of a program) concurrently, in the same process.

What does %s mean in a Python format string?

In a Python format string, the %s placeholder represents a string.

How do I check file size in Python?

To check the size of a file in Python, you can use the os module to get the size of the file in bytes.

How to put the legend outside the plot

In matplotlib, the legend function allows you to specify the location of the legend.

error: Unable to find vcvarsall.bat

The error: Unable to find vcvarsall.bat message is usually raised when you are trying to install a Python package that requires a C compiler, and the compiler is not installed or cannot be found by Python.

TypeError: 'NoneType' object is not iterable in Python

The TypeError: 'NoneType' object is not iterable error message is raised when you are trying to iterate over an object that has a value of None, which is not iterable.

How to list all functions in a module?

To list all functions in a module, you can use the dir() function to get a list of all the names defined in the module, and then use the inspect module to check if each name is a function.

How do I check if directory exists in Python?

You can use the os module to check if a directory exists.

Asking the user for input until they give a valid response

To ask the user for input and repeat the prompt until they give a valid response, you can use a while loop and use a try-except block to catch any errors that may occur when trying to convert the user input to the desired data type.

how to sort pandas dataframe from one column

To sort a Pandas DataFrame based on the values in a column, you can use the sort_values() method of the DataFrame.

How to change the order of DataFrame columns?

To change the order of columns in a Pandas DataFrame, you can use the DataFrame's "reindex" method and specify the new order of the columns.

What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

The "SyntaxError: Missing parentheses in call to 'print'" error message is raised when you are using Python 3 and you have forgotten to include the parentheses when calling the print() function.

How do I remove a substring from the end of a string?

To remove a substring from the end of a string, you can use the rsplit() method and specify the substring as the delimiter.

Import error: No module name urllib2

It looks like you are trying to import the urllib2 module, which is not available in Python 3.

Convert Pandas Column to DateTime

To convert a column in a Pandas DataFrame to a datetime data type, you can use the pandas.to_datetime() function.

How to use glob() to find files recursively?

You can use the glob.glob() function from the glob module to search for files recursively.

What are the differences between type() and isinstance()?

In Python, type() is a built-in function that returns the type of an object, while isinstance() is a function that checks whether an object is an instance of a particular class or of a subclass thereof.

How can I check for NaN values?

You can use the isnan() function provided by the math module to check if a value is NaN.

Create a dictionary with comprehension

To create a dictionary using comprehension in Python, you can use the following syntax:

if/else in a list comprehension

Here is an example of using an if/else statement in a list comprehension in Python:

How can I flush the output of the print function?

To flush the output of the print function, you can use the flush parameter of the print function.

Use of *args and **kwargs

In Python, *args and **kwargs are special keywords that allow you to pass a variable number of arguments to a function.

Creating a singleton in Python

A singleton is a design pattern that allows you to ensure that a class has only one instance.