3

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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'])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.