How to apply rules in python, if i want A, B = 1,2 and C,D = 3,4 and E,F = 5,6 each and drop the remaining
Type Set
1 A 1
2 B 2
3 B 3
4 C 4
5 D 5
6 A 2
7 F 3
8 F 2
9 E 1
10 D 5
11 E 5
12 C 6
i tried using drop but its lengthy
What about using multiple masks:
m1 = df['Type'].isin(['A', 'B'])
m2 = df['Type'].isin(['C', 'D'])
m3 = df['Set'].isin([1, 2])
m4 = df['Set'].isin([3, 4])
out = df.loc[(m1&m3)|(m2&m4)]
Or:
m1 = df['Type'].isin(['A', 'B'])
m2 = df['Type'].isin(['C', 'D'])
m3 = df.loc[m1, 'Set'].isin([1, 2]).reindex(df.index, fill_value=False)
m4 = df.loc[m2, 'Set'].isin([3, 4])
out = df.loc[m3 | m4]
Output:
Type Set
1 A 1
2 B 2
4 C 4
6 A 2
7 C 3
9 B 1
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.