Open In App

How to display notnull rows and columns in a Python dataframe?

In Python, not null rows and columns mean the rows and columns which have Nan values, especially in the Pandas library. To display not null rows and columns in a python data frame we are going to use different methods as dropna(), notnull(), loc[].

Steps:



Below StudentData.csv file used in the program:



Method 1: Using dropna() method 

In this method, we are using the dropna() method which drops the null rows and displays the modified data frame.




# Import library
import pandas as pd
  
# Reading csv file
df = pd.read_csv('StudentData.csv')
  
# using dropna() method
df = df.dropna()
  
# Printing result
print(df)

Output:

Method 2: Using notnull() and dropna() method

In this method, we will first use notnull() method which is return a boolean object having True and False values. If there is NaN value it will return false otherwise true. And then give these boolean objects as input parameters to where function along with this function use drpna() to drop NaN rows.




# Import library
import pandas as pd
  
# Reading csv file
df = pd.read_csv('StudentData.csv')
  
# using notnull() method ,it will return 
# boolean values
mask = df.notnull()
  
# using dropna() method to drop NaN 
# value rows
df = df.where(mask).dropna()
  
# Displaying result
print(df)

Output:

Method 3: Using loc[] and notnull() method

In this method, we are using two concepts one is a method and the other is property. So first, we find a data frame with not null instances per specific column and then locate the instances over whole data to get the data frame.




# Import library
import pandas as pd
  
# Reading csv file
df = pd.read_csv('StudentData.csv')
  
# Here filtering the rows according to 
# Grade column which has notnull value.
df = df.loc[df['Grade'].notnull()]
  
# Displaying result
print(df)

Output:

As shown in the output image, only the rows having Grade != NaN is displayed.


Article Tags :