I have a pandas dataframe with three columns, A (Dates), B (Categorical Values), and C (Actual Values).
A | B | C
01-19 | 5 | 0.34
01-19 | 3 | 0.25
01-19 | 7 | 0.07
01-20 | 5 | 0.15
01-20 | 2 | 0.36
And so on.
What I want is to filter rows according to their dates and a specific threshold - something like:
[(01-19, x<6), (01-20, x<3)]
In which case that'd give me
A | B | C
01-19 | 5 | 0.34
01-19 | 3 | 0.25
01-20 | 2 | 0.36
My solution is to set up a multi index with A and B, but then I'm not entirely sure how to filter through the B's.
Idea is create dictionary of tresholds for all values of A
column, then Series.map
to new Series, so possible compare by B
column and filter by boolean indexing
:
d = {'01-19': 6, '01-20' : 3}
df = df[df['B'] < df['A'].map(d)]
print (df)
A B C
0 01-19 5 0.34
1 01-19 3 0.25
4 01-20 2 0.36
Detail :
print (df['A'].map(d))
0 6
1 6
2 6
3 3
4 3
Name: A, dtype: int64
If only two conditional you can you "and" and "or" operators like that:
df = df[((df['A'] == '01-19') & (df['B'] < 6)) | ((df['A'] == '01-20') & (df['B'] < 3))]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.