How to Drop Rows with NaN Values in Pandas DataFrame

To drop rows with NaN (null) values in Pandas DataFrame: Copy df.dropna() To drop rows where all the values are NaN: Copy df.dropna(how=”all”) Steps to Drop Rows with NaN Values in Pandas DataFrame Step 1: Create a DataFrame with NaN Values Create a DataFrame with NaN values: Copy import pandas as pd import numpy as … Read more

Append an Item to a List in Python

To append an item at the end of a list: Copy list_name.append(“new item”) To add multiple items to a list: Copy list_name.extend([“item_1”, “item_2”, “item_3”, …]) Steps to Append an Item to a List in Python Step 1: Create a List To start, create a list in Python. For instance: Copy product_list = [“Oven”, “Toaster”, “Dishwasher”, … Read more

How to Rename Columns in Pandas DataFrame

To rename a single column in Pandas DataFrame: Copy df.rename(columns={‘old column name’: ‘new column name’}, inplace=True) To rename multiple columns in Pandas DataFrame: Copy df.rename(columns={‘old column 1’: ‘new column 1’, ‘old column 2’: ‘new column 2′}, inplace=True) Example 1: Rename a Single Column in Pandas DataFrame Let’s say that you created a DataFrame in Python, … Read more

How to Reset an Index in Pandas DataFrame

To reset an index in Pandas DataFrame: Copy df.reset_index(drop=True, inplace=True) Steps to Reset an Index in Pandas DataFrame Step 1: Gather your data For illustration purposes, let’s gather the following data about different products: Product Price Tablet 250 Printer 150 Laptop 1200 Monitor 300 Computer 1500 Step 2: Create a DataFrame Next, create a DataFrame … Read more

Convert a List to Pandas Dataframe (with examples)

At times, you may need to convert a list to Pandas DataFrame in Python. You may then use the following template to convert your list to a DataFrame: Copy import pandas as pd list_name = [‘item_1’, ‘item_2’, ‘item_3’, …] df = pd.DataFrame(list_name, columns=[‘column_name’]) In the next section, you’ll see how to perform the conversion in … Read more

Modify an Item Within a List in Python

To modify an item within a list in Python: Copy list_name[Index of the item to be modified] = “New value for the item” Where the index of the first item in the list is zero, and then increases sequentially. Steps to Modify an Item Within a List in Python Step 1: Create a List To … Read more

How to Create a List in Python

To create a list in Python: Copy list_name = [‘item1’, ‘item2’, ‘item3’, ….] Examples Here are examples of two lists in Python: (1) List of products – this list contains strings (by placing the values within quotes): Copy products = [“microwave”, “oven”, “toaster”, “refrigerator”, “dishwasher”] (2) List of prices – this list contains numbers (i.e., … Read more

Convert Strings to Integers in Pandas DataFrame

Here are 3 approaches to convert strings to integers in Pandas DataFrame: (1) The astype(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].astype(int) (2) The apply(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].apply(int) (2) The map(int) approach: Copy df[“dataframe_column”] = df[“dataframe_column”].map(int) Let’s review an example with the steps to convert strings to integers. Steps to Convert Strings to Integers in … Read more

Join Pandas DataFrames using Merge

To join Pandas DataFrames using merge: Copy pd.merge(df1, df2, how=’type of join’, on=[‘df1 key’, ‘df2 key’]) In this short guide, you’ll see the steps create the following joins: Inner Left Right Outer Steps to Join Pandas DataFrames using Merge Step 1: Create the DataFrames to be joined Let’s say that you have two datasets that … Read more

How to Compare Values between two Pandas DataFrames

In this short guide, you’ll see how to compare values between two Pandas DataFrames. You’ll also observe how to compare values from two imported files. Steps to Compare Values Between two Pandas DataFrames Step 1: Prepare the datasets to be compared To start, let’s say that you have the following two datasets that you want … Read more