How to use dropna() In Python Pandas
We can drop Rows having NaN Values in Pandas DataFrame by using dropna() function
df.dropna()
It is also possible to drop rows with NaN values with regard to particular columns using the following statement:
df.dropna(subset, inplace=True)
With in place set to True and subset set to a list of column names to drop all rows with NaN under those columns.
Let’s make our own Dataframe and remove the rows with NaN values so that we can clean data.
Python3
import pandas as pd import numpy as np data = pd.DataFrame({ 'A' : [ 1 , 2 , np.nan, 4 ], 'B' : [ 5 , 6 , 7 , 8 ], 'C' : [ 10 , 11 , 12 , np.nan], 'D' : [ 21 , 22 , 23 , 24 ]}) print (data) |
Output:
A B C D
0 1.0 5 10.0 21
1 2.0 6 11.0 22
2 NaN 7 12.0 23
3 4.0 8 NaN 24
Python3
data = data.dropna() # drop rows with nan values print (data) |
Output:
A B C D
0 1.0 5 10.0 21
1 2.0 6 11.0 22
How to Drop Rows with NaN Values in Pandas DataFrame?
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. In this article, we will discuss how to drop rows with NaN values.