How to Convert Pandas DataFrame to NumPy Array

Here are two approaches to convert Pandas DataFrame to a NumPy array: (1) First approach: Copy df.to_numpy() (2) Second approach: Copy df.values Steps to Convert Pandas DataFrame to a NumPy Array Step 1: Create a DataFrame To start with a simple example, let’s create a DataFrame with 3 columns. The 3 columns will contain only … Read more

Delete Records in SQL Server using Python

In this guide, you’ll see how to delete records in SQL Server directly from Python. Here are the steps that you may follow. Steps to Delete Records in SQL Server using Python Step 1: Install the Pyodbc Library If you haven’t already done so, install the pyodbc library using the following command: Copy pip install … Read more

Select all Rows with NaN Values in Pandas DataFrame

Here are 4 ways to select all rows with NaN values in Pandas DataFrame: (1) Using isna() to select all rows with NaN under a single DataFrame column: Copy df[df[‘column name’].isna()] (2) Using isnull() to select all rows with NaN under a single DataFrame column: Copy df[df[‘column name’].isnull()] (3) Using isna() to select all rows … Read more

How to Get the Max Value using SQL

To find the max value using SQL: Copy SELECT MAX(field_name) AS new_field_name FROM table_name Next, you’ll see how to find the max value across the following 3 scenarios: Under a single field After joining tables After using group by (1) Find the Max Value Under a Single Field To start with a simple example, let’s … Read more

How to Filter Pandas DataFrame Based on Index

Here is the syntax that you can use to filter Pandas DataFrame based on the index: Copy df = df.filter(items = [index to keep], axis=0) Let’s review an example to see how to apply the above syntax in practice. The Example Suppose that you created the DataFrame below: Copy import pandas as pd data = … 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

How to Find Where Python is Installed on Windows

In this short guide, you’ll see two methods to find where Python is installed on Windows: Find Where Python is Installed on Windows using the Sys Library You can use the sys library in order to find where Python is installed: Copy import sys print(sys.exec_prefix) Here is an example of a path structure that you … 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

How to Sort an Index in Pandas DataFrame

You can sort an index in Pandas DataFrame: (1) In an ascending order: Copy df = df.sort_index() (2) In a descending order: Copy df = df.sort_index(ascending=False) Let’s see how to sort an index by reviewing an example. The Example To start, let’s create a simple DataFrame: Copy import pandas as pd data = {‘Product’: [‘Computer’,’Printer’,’Monitor’,’Desk’,’Phone’,’Tablet’,’Scanner’], … 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