How to remove rows with null values from kth column onward?

Learn, how to remove rows with null values from kth column onward in Python? 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 the dataframe with some columns and we need to remove all rows in which elements from column 2 onwards are all NaN.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean “Not a Number” which generally means that there are some missing values in the cell.

Removing rows with null values from kth column onward

For this purpose, we will simply use the dropna() method where we will use the argument subset and we will assign a list of all those columns from which we want to drop Nan values.

Let us understand with the help of an example,

Python program to remove rows with null values from kth column onward

# Importing pandas
import pandas as pd

# Import numpy
import numpy as np

# Creating a dataframe
df = pd.DataFrame({
    'one':[1,2,3,4,5],
    'two':[1,np.nan,3,4,5],
    'three':[1,2,3,4,5],
    'four':[1,2,3,4,5],
    'five':[1,2,3,4,5]},index=['a','c','e','g','h'])

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

# Dropping nan values from column 3rd
df.dropna(subset=['two','three','four','five'],how='all',inplace=True)

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

Output

The output of the above program is:

Example: How to remove rows with null values from kth column onward?

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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