Home > OS >  How could i drop rows in Pandas in condition?
How could i drop rows in Pandas in condition?

Time:01-03

For example, let's take Penguins dataset, and i want to drop all entries in bill_length_mm column when they are more then 30:

import seaborn as sns
import pandas as pd

ds = sns.load_dataset("penguins")
ds.head()

ds.drop(ds[ds['bill_length_mm']>30]) 

And it gives me an error. And if i'll try to add axis=1 it'll just drop every column in dataset.

ds.drop(ds[ds['bill_length_mm']>30], axis=1)

So what shoud i do to complete ma goal?

CodePudding user response:

Try
ds=ds.drop(ds[ds['bill_length_mm']>30].index)

Or
ds = ds[ds['bill_length_mm']<=30]

CodePudding user response:

ds.drop is used to drop columns, not rows. If you only want to keep the rows where bill_length_mm<=30, you can use

ds = ds[ds['bill_length_mm']<=30]
  • Related