How to display notnull rows and columns in a Python dataframe?
Last Updated :
03 Mar, 2021
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[].
- dropna() : This function is used to remove rows and column which has missing values that are NaN values. dropna() function has axis parameter. If it set to 0 then it will remove all the rows which have NaN value and if it is set to 1 then it will remove all the columns which have NaN value. By default, the value of axis parameter is 0.
- notnull(): This function detects non-missing values and returns a mask of boolean values for each element in DataFrame that indicates whether an element is not an NA value.
- loc[]: This method filter rows and columns by labels or boolean array. In our example, this method filters rows by a boolean array which is returned by notnull() method.
Steps:
- Import pandas library
- Read the CSV file, or you can create your own data frame.
- Use one of the method like dropna(), notnull(), loc[] as described below.
- Display result
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.
Python3
# 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.
Python3
# 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.
Python3
# 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.
Similar Reads
How to display bar charts in Pandas dataframe on specified columns? In this article we will see how to display bar charts in dataframe on specified columns. For doing this task we are using DataFrame.style.bar() method of Pandas Dataframe. Syntax: pandas.DataFrame.style.bar(columns_list, color) Return: Dataframe with the given color bar strips on the positive defini
1 min read
How to drop all columns with null values in a PySpark DataFrame ? The pyspark.sql.DataFrameNaFunctions class in PySpark has many methods to deal with NULL/None values, one of which is the drop() function, which is used to remove/delete rows containing NULL values in DataFrame columns. You can also use df.dropna(), as shown in this article. You may drop all rows in
3 min read
How to get column and row names in DataFrame? While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations. Note: For downloading the nba dataset used in the below examples Click Here Getting row names in Pandas dataframe First, let's
3 min read
How to widen output display to see more columns in Pandas dataframe? When working with pandas DataFrames, the default print settings often truncate rows, columns, or even the contents of cells. By default only 20 columns are shown. Columns beyond this limit are replaced with ellipses (...) for hidden rows or columns making it difficult to view the entire dataset. To
3 min read
How to add Empty Column to Dataframe in Pandas? In Pandas we add empty columns to a DataFrame to create placeholders for future data or handle missing values. We can assign empty columns using different methods depending on the type of placeholder value we want. In this article, we will see different methods to add empty columns and how each one
2 min read
How to Show All Columns of a Pandas DataFrame? Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read