How-to articles, tricks, and solutions about PYTHON

Extract file name from path, no matter what the os/path format

You can use the os.path.basename() function to extract the file name from a file path, regardless of the operating system or path format.

Extracting extension from filename in Python

You can extract the file extension from a file name in Python by using the os.path module.

Extracting just Month and Year separately from Pandas Datetime column

You can extract the month and year separately from a Pandas datetime column using the dt accessor.

Extracting specific selected columns to new DataFrame as a copy

To create a new DataFrame with a subset of columns from an existing DataFrame, you can use the pandas library.

Fastest way to check if a value exists in a list

The fastest way to check if a value exists in a list is to use the in operator.

fatal error: Python.h: No such file or directory

It looks like you are trying to include the Python.h header file in a C or C++ program, but it is not found on your system.

Filter pandas DataFrame by substring criteria

Here is an example of how you can filter a pandas DataFrame by substring criteria:

Find all files in a directory with extension .txt in Python

Here is a code snippet that uses the os and glob modules to find all files in a directory with the extension '.txt':

Find the current directory and file's directory

To find the current directory in Python, you can use the following code:

Find which version of package is installed with pip

You can use the following command to check the version of a package installed with pip:

Finding and replacing elements in a list

Here is a Python code snippet that demonstrates how to find and replace elements in a list:

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:

Finding the average of a list

Here is a Python code snippet for finding the average of a list of numbers:

Finding the index of an item in a list

To find the index of an item in a list in Python, you can use the index() method of the list.

Generate random integers between 0 and 9

To generate a random integer between 0 and 9 in Python, you can use the random module and the randint function.

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.

Get difference between two lists with Unique Entries

You can use the set data type to find the difference between two lists with unique entries.

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:

Get key by value in dictionary

You can use the in keyword to check if a value exists in a dictionary, and then use the items() method to return a list of key-value pairs.

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.

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

In pandas, you can use the groupby() method to group data by one or more columns and then use the agg() method to compute various statistics for each group.

Get the data received in a Flask request

In a Flask application, you can access the data received in a request using the request object.

Get unique values from a list in python

In Python, you can use the set() function to get the unique values from a list.

Getting a list of all subdirectories in the current directory

Here is a code snippet in Python that gets a list of all subdirectories in the current directory:

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.