Select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns

Given a pandas dataframe, we have to select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns. By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we are given a dataframe with several columns and we need to drop some of these columns in such a way that a specific column in the main data frame is Nan.

Selecting non-null rows from a specific column in a DataFrame

For this purpose, we will pass a Boolean mask on our data frame on that specific column, and then we will pass this mask to the loc property to take only the Nan rows.

Let us understand with the help of an example,

Python program to select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'a':[45,67,64,24,75],
    'b':[20,21,22,np.nan,20],
    'c':[34,64,24,87,53]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display Original df
print("Original DataFrame:\n",df,"\n")

# passing boolean mask in loc
res = df.loc[df['b'].notnull(), ['a','c']]

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Select non-null rows from a specific column in a DataFrame and take a sub-selection of other columns

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.