Introduction

In this tutorial, we want to drop rows with null values from a Pandas DataFrame. In order to do this, we use the the dropna() method of Pandas.

Import Libraries

First, we import the following python modules:

import numpy as np
import pandas as pd

Create Pandas DataFrame

Next, we create a Pandas DataFrame with some example data from a dictionary:

mydict = {
    "language": ["Python", "JavaScript", "Python", "Java", np.nan],
    "framework": ["FastAPI", np.nan, "Django", np.nan, np.nan],
    "users": [np.nan, 7000, 20000, np.nan, np.nan],
}
df = pd.DataFrame(mydict)
df

Remove Rows with Missing Values in any Column

Next, we would like to remove all rows from the DataFrame that have null values in any column.

To do this, we use the dropna() method of Pandas. We have to use the how parameter and pass the value "any" as argument:

df_cleaned = df.dropna(how="any")
df_cleaned

Removing Rows with Null Values in all Columns

Next, we would like to remove all rows from the DataFrame that have null values in all columns.

To do this, we use the dropna() method of Pandas. We have to use the how parameter and pass the value "all" as argument:

df_cleaned = df.dropna(how="all")
df_cleaned

Removing Rows with Missing Values in a certain Column

Next, we would like to remove all rows from the DataFrame that have null values in the column "framework".

To do this, we use the dropna() method of Pandas. We have to use the subset parameter and pass the column name as argument:

df_cleaned = df.dropna(subset=["framework"])
df_cleaned

Conclusion

Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to drop rows with null values from a Pandas DataFrame. We can simply use the dropna() method of Pandas. Try it yourself!

Instagram

Also check out our Instagram page. We appreciate your like or comment. Feel free to share this post with your friends.