Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

python - How to drop entire group in pandas if any other columns meet certain criteria?

I have a df that looks like this:

Name  Letter  Period  Amount
123   H       PRE     11 
123   H       DURING  5
123   H       POST    100
456   H       PRE     9
456   H       DURING  50
456   H       POST    600
789   J       PRE     8
789   J       DURING  9
789   J       POST    200

Currently, I am using this line of code to filter on the df so that only rows that are of a period PRE and have an amount of more than 10 are included:

revised_data[ (revised_data['Period'] == 'PRE' ) & (revised_data['Amount'] > 10)]

What I realized though is that I actually need to remove the entire grouping from the df if even just the PRE period doesn't satisfy the > 10 condition. So in that case I would need all 456 rows and 789 rows removed just because their PRE period row was below 10. How might I adjust my code to accomplish this?

Expected Output:

Name  Letter  Period  Amount
123   H       PRE     11 
123   H       DURING  5
123   H       POST    100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please try:

df.loc[df['Name'].isin(df['Name'].loc[ (df['Period'] == 'PRE' ) & (df['Amount'] > 10)])]

Prints:

   Name Letter  Period  Amount
0   123      H     PRE      11
1   123      H  DURING       5
2   123      H    POST     100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...