Geek Logbook

Tech sea log book

How to Drop Rows Based on a Column Value in a pandas DataFrame

Problem Statement

Let’s say you have a DataFrame containing weather data, and you want to drop all rows where the quantity (qty) is less than 5. However, you notice that some rows are being dropped incorrectly or the DataFrame remains unchanged after applying the filtering conditions. We’ll show you how to correctly drop the rows and reset the index.

Sample Data

Here’s a sample of the CSV data we’ll be working with:

YEAR,MO,GP,HR,qty,SKYMEAN,SKY100MEAN,SKY100STD,SKY100MEDIAN
1973,1,1,0,1,7,87.5,,87.5
1973,1,1,14,1,8,100,,100
1973,1,1,15,4,5.25,65.625,43.75,87.5
1973,1,1,16,12,4.416666667,55.20833333,48.98351222,87.5
1973,1,1,17,22,5.227272727,65.34090909,43.80214888,87.5
1973,1,1,18,21,5.095238095,63.69047619,39.70688737,87.5
1973,1,1,19,20,5.2,65,39.23612728,87.5
1973,1,1,20,32,5.15625,64.453125,36.63097565,87.5
1973,1,1,21,19,5.526315789,69.07894737,37.5851859,87.5
1973,1,1,22,12,6.083333333,76.04166667,35.93647067,87.5
1973,1,1,24,144,5.263888889,65.79861111,39.13917785,87.5
1973,2,1,0,1,8,100,,100
1973,2,1,15,1,7,87.5,,87.5
1973,2,1,16,10,5.4,67.5,35.93976442,87.5
1973,2,1,17,24,4.458333333,55.72916667,44.83956404,87.5
1973,2,1,18,20,4.4,55,42.2243382,56.25
1973,2,1,19,21,4.714285714,58.92857143,43.50697809,87.5
1973,2,1,20,26,5,62.5,43.30127019,87.5
1973,2,1,21,22,5.636363636,70.45454545,39.8183103,87.5
1973,2,1,22,4,6,75,33.85016002,87.5
1973,2,1,23,2,7.5,93.75,8.838834765,93.75
1973,2,1,24,131,5.007633588,62.59541985,41.32703132,87.5
import pandas as pd

# Load data from CSV
sample_data = pd.read_csv('path_to_your_csv_file.csv')

# Drop rows where qty < 5
sample_data.drop(sample_data[sample_data['qty'] < 5].index, inplace=True)

# Reset the index
sample_data = sample_data.reset_index(drop=True)

# Display the resulting DataFrame
print(sample_data)

Expected Result

The resulting DataFrame should only include rows where qty is 5 or greater, with the index reset.

YEAR,MO,GP,HR,qty,SKYMEAN,SKY100MEAN,SKY100STD,SKY100MEDIAN
1973,1,1,16,12,4.416666667,55.20833333,48.98351222,87.5
1973,1,1,17,22,5.227272727,65.34090909,43.80214888,87.5
1973,1,1,18,21,5.095238095,63.69047619,39.70688737,87.5
1973,1,1,19,20,5.2,65,39.23612728,87.5
1973,1,1,20,32,5.15625,64.453125,36.63097565,87.5
1973,1,1,21,19,5.526315789,69.07894737,37.5851859,87.5
1973,1,1,22,12,6.083333333,76.04166667,35.93647067,87.5
1973,1,1,24,144,5.263888889,65.79861111,39.13917785,87.5
1973,2,1,16,10,5.4,67.5,35.93976442,87.5
1973,2,1,17,24,4.458333333,55.72916667,44.83956404,87.5
1973,2,1,18,20,4.4,55,42.2243382,56.25
1973,2,1,19,21,4.714285714,58.92857143,43.50697809,87.5
1973,2,1,20,26,5,62.5,43.30127019,87.5
1973,2,1,21,22,5.636363636,70.45454545,39.8183103,87.5
1973,2,1,23,2,7.5,93.75,8.838834765,93.75
1973,2,1,24,131,5.007633588,62.59541985,41.32703132,87.5
Tags: