Pandas isin
NAME Course Score Time mark
0 XIAOMING ENG 60 2020-12-30 a
1 XIAOHONG MATH 25 2020-12-31 b
2 XIAOLI CHI 5 2021-01-01 b
3 XIAOLI CHI 5 2021-01-01 c
4 XIAOLI CHI 5 2021-01-01 d
import pandas as pd
df=pd.read_excel(r'C:\Users\Administrator\Desktop\1.xlsx')
print(df)
#通过isin过滤掉Course小于2次的科目
df_new=df.groupby('Course').count()['NAME'] #通过groupby筛选Course
print(df_new)
print(df_new[df_new>2]) #得到Course出现2次的bool
print(df[df['Course'].isin(df_new[df_new > 2].index.values)])
print(df[~df['Course'].isin(df_new[df_new > 2].index.values)])