Drop Rows with NaN Values in Pandas DataFrame
NaN stands for Not A Number . Pandas DataFrame treat None values and NaN as essentially interchangeable for showing missing or null values . Missing values is a very big problem in real life cases. In some cases you have to find and remove this missing values from DataFrame. Pandas dropna() method allows you to find and delete Rows/Columns with NaN values in different ways.
First let's create a data frame with values.
How to check if any value is NaN in a Pandas DataFrame
Above output shows how many null values is each column in a DataFrame.
How to drop all rows that have at least one NaN values
Above output returned only one row because at least one NaN values in every other rows.
The axis parameter tells the dropna() function whether you want to drop rows (axis=0) or drop columns (axis=1).
Above output returned no rows because all column have at least one NaN value.
How to drop a row whose particular column is NaN?
You can use dropna() with parameter subset for specify column for check NaNs:
In the above output, the second and fourth row is missing because in that row the 'TotalMarks' column have NaN values .
If you want to find a particular column have NaN values:
Above output returned two rows. This means that the column 'TotalMarks' have two NaN value.
How to drop rows only if ALL columns are NaN
Here you can see the fourth row is missing because in that particular rows all column value have NaN values .
How to drop row if it does not have at least two values that are not NaN
Here also you can see the fourth rows is missing because it has more than two NaN values .