Pandas is a popular python library to import, analyze, transform and export data in Python. It allows you to work with data using dataframes that are similar tables with rows, columns and headers. Sometimes you may need to drop rows in Pandas with NaN values. This is a common problem especially if you have imported data from other source to your dataframe. Often external data sources are not in the right format and lead to NaN values all over the resultant dataframe, making them less useful. In this article, we will learn how to drop rows in Pandas with NaN values.
How to Drop Rows in Pandas With NaN Values
Let us say you have the following dataframe in Pandas.
>>> df STK_ID EPS cash 601166 NaN NaN 600036 NaN 12 600016 4.3 NaN 601009 NaN NaN 601939 2.5 NaN 000001 NaN NaN
Let us say you want to drop rows where EPS is not NaN.
df = df[df['EPS'].notna()]
In the above command, we call notna() function on EPS column to allow only rows where EPS is not null.
Alternatively, you can also use notnull() function here. In the following example, we call notnull() function on EPS column to permit rows where EPS is not null.
import pandas as pd df = df[pd.notnull(df['EPS'])]
You can also use dropna() function to drop rows with NaN values.
Please note, each of the above functions will drop rows with both null as well as NaN values.
In this article, we have learnt 3 simple ways to drop rows in Pandas with NaN values.