Filter Pandas DataFrame Based on Index

To filter Pandas DataFrame based on the index: Copy df_filter = df.filter(items=[index to keep], axis=0) Example Suppose that you created the DataFrame below: Copy import pandas as pddata = { “Product”: [“Computer”, “Printer”, “Monitor”, “Desk”, “Phone”, “Tablet”, “Scanner”], “Price”: [900, 200, 300, 450, 150, 250, 150],}df = pd.DataFrame(data)print(df) As you can see in yellow, the … Read more

Check if Pandas DataFrame is Empty

You can use df.empty to check if Pandas DataFrame is empty: Copy df.empty Where: Steps to Check if Pandas DataFrame is Empty Step 1: Create a DataFrame To start with a simple example, let’s create a DataFrame with 2 columns: Copy import pandas as pd data = {‘Color’: [‘Blue’, ‘Blue’, ‘Green’, ‘Green’, ‘Red’, ‘Red’], ‘Height’: … Read more

Screenshot to PDF using Python

In this short guide, you’ll see the full steps to take a screenshot and then convert it to PDF using Python. To accomplish this goal you’ll need to use the following packages: Steps to take a screenshot and then convert it to PDF using Python Step 1: Install the relevant packages If you haven’t already … Read more

Sort an Index in Pandas DataFrame

You can sort an index in Pandas DataFrame: (1) In an ascending order: Copy df.sort_index(inplace=True) (2) In a descending order: Copy df.sort_index(inplace=True, ascending=False) The Example To start, create a simple DataFrame: Copy import pandas as pddata = { “Product”: [“Computer”, “Printer”, “Monitor”, “Desk”, “Phone”, “Tablet”, “Scanner”], “Price”: [1100, 150, 500, 350, 100, 300, 120],}df = … Read more

Drop Rows by Index in Pandas DataFrame

Here are two ways to drop rows by the index in Pandas DataFrame: (1) Drop a single row by index. For example, to drop the row that has an index of 2: Copy df.drop(index=2, inplace=True)  (2) Drop multiple rows by index. For instance, to drop the rows with the index values of 2, 4 and … Read more

Export SQL Server Table to CSV using Python

In this guide, you’ll see the full steps to export SQL Server table to a CSV file using Python. The Example Assume that you want to export the following table (called the ‘dbo.product‘ table) from SQL Server to CSV using Python: product_id product_name price 1 Computer 800 2 TV 1200 3 Printer 150 4 Desk … Read more

Drop Columns from Pandas DataFrame

Here are two approaches to drop columns from Pandas DataFrame (1) Drop a single column from the DataFrame: Copy df.drop(‘column name’, axis=1, inplace=True) (2) Drop multiple columns from the DataFrame: Copy df.drop([‘column 1’, ‘column 2’, ‘column 3′, …], axis=1, inplace=True) The Example To start with a simple example, let’s create a DataFrame with 5 columns: … Read more

How to Write a String to a Text File using Python

To write a string to a text file using Python: Copy text_file = open(r”path where the text file will be created\file name.txt”, “w”)my_string = “type your string here”text_file.write(my_string)text_file.close() In this short guide, you’ll see how to: Steps to Write a String to a Text File using Python Step 1: Specify the path for the text … Read more

Randomly Select Columns in Pandas DataFrame

Here are 4 ways to randomly select columns in Pandas DataFrame: (1) Randomly select a single column: Copy df = df.sample(axis=”columns”) (2) Randomly select a specified number of columns. For example, to select 3 random columns, set n=3: Copy df = df.sample(n=3, axis=”columns”)  (3) Allow a random selection of the same column more than once … Read more