Remove rows in less than a certain value

Given a pandas dataframe, we have to remove rows in less than a certain value. By Pranit Sharma Last updated : October 03, 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 multiple columns of integer values and we need to filter out the data of this DataFrame which is less than a certain value.

Removing rows in less than a certain value

Assuming that certain values are constant throughout this operation, we will use the simple filtering process of selecting some values from DataFrame along with a condition related to the fixed value.

We will use the tilde sign operator for this purpose, tilde sign (~) in pandas is used when we work with Boolean values. In programming, we sometimes use some specific values that only have two values, either True or False. These values are known as Boolean values.

Let us understand with the help of an example,

Python program to remove rows in less than a certain value

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'A': [1,2,5,6,9,10],
    'B': [3,4,7,8,11,12]
}

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

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

# Filtering DataFrame
res = df[~(df['B'] <= 10)]

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

Output

The output of the above program is:

Example: Remove rows in less than a certain value

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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