Issue
Is it possible to subset a Pandas 1.3.4 dataframe by multiple row conditions for a single value? To make this clearer, if I have a df
:
index col1 col2
0 A 10
1 A 20
2 B 130
3 C 10
If I want to subset based on a col2
value I can do df[df['col2']>10]
:
index col1 col2
1 A 20
2 B 130
But is it possible to subset col2
using different thresholds based on the col1
value?
For example:
df[df['col2']>10 if col1 == 'A'
OR df['col2']>5 if col1 == 'C'
OR df['col2']>1000 if col1 == 'B']`
would give:
index col1 col2
1 A 20
3 C 10
Thanks!
Tim
Solution
If need compare for greater all values use Series.map
by dictionary and compare:
d = {'A' : 10,'C': 5 ,'B': 1000}
df[df['col2'] > df['col1'].map(d)]
Or if need different masks chain by &
for bitwise AND
and |
for bitwise OR
:
df[((df['col2']>10) & (df['col1']== 'A') ) |
((df['col2']>5) & (df['col1']== 'C') ) |
((df['col2']>1000) & (df['col1']== 'B') )]
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.