Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how select pandas rows based on condition on other columns?

Tags:

python

pandas

I have a dataframe that looks like the following

df
     city   val
0   London   3
1   London   -1
2   London   -1
3   Paris    -5
4   Paris    -2
5   Rome     2
6   Rome     2

I want to select only the city that have at least one val < 0. I would like to have the following:

df
     city   val
0   London   3
1   London   -1
2   London   -1
3   Paris    -5
4   Paris    -2
like image 326
emax Avatar asked May 22 '26 01:05

emax


2 Answers

Use loc with groupby and .transform(min):

>>> df.loc[df.groupby('city')['val'].transform(min).lt(0)]
     city  val
0  London    3
1  London   -1
2  London   -1
3   Paris   -5
4   Paris   -2
>>> 

Since you're filtering under 0, just filter if the minimum value of each group is lower than 0.

like image 173
U12-Forward Avatar answered May 24 '26 14:05

U12-Forward


Create mask and filter rows with GroupBy.transform:

df = df[df['val'].lt(1).groupby(df['city']).transform('any')]
print (df)
     city  val
0  London    3
1  London   -1
2  London   -1
3   Paris   -5
4   Paris   -2

Or filter city with at least one row match less like 1 and filter original city column by Series.isin:

df[df['city'].isin(df.loc[df['val'].lt(1), 'city'])]
like image 31
jezrael Avatar answered May 24 '26 15:05

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!