【发布时间】:2022-09-30 23:18:03
【问题描述】:
我有一个类似于这个的数据框
df = pd.DataFrame({\'date\':[20220101,20220102,20220103,20220101,20220102,20220101], \'id\':[1,1,1,2,2,3], \'value\':[11,22,33,44,55,66], \'categorie\':[\'a\',\'a\',\'c\',\'a\',\'c\',\'c\']})
date id value categorie
20220101 1 11 a
20220102 1 22 a
20220103 1 33 c
20220101 2 44 a
20220102 2 55 c
20220101 3 66 c
我现在想根据列 \'categorie\' 中的多个值对 df 进行切片,并且目前正在使用
df = df[df[\'categorie\'].isin([\'a\',\'c\'])]
除此之外,我希望能够只为类别 \'a\' 获得 [-1] 行
date id value categorie
20220102 1 22 a
20220103 1 33 c
20220101 2 44 a
20220102 2 55 c
20220101 3 66 c
代替
date id value categorie
20220101 1 11 a
20220102 1 22 a
20220103 1 33 c
20220101 2 44 a
20220102 2 55 c
20220101 3 66 c
我认为最接近的方法是将其视为 id 和 categorie 的 groupby 最大值,但我很好奇是否有更 Pythonic 的方式。