python和pandas处理excel(7)

2020-09-02  本文已影响0人  OURPIECE

筛选和过滤

ID age score
1 16 80
2 15 96
3 18 88
4 16 75
5 23 90
6 12 65

场景1

import pandas as pd
def age_18to30(a):
  return 18 <= a < 30
def level_a(s):
  return 80 <= s <=100
df = pd.read_excel("E:/test.xlsx",index_col='ID')
df = df.loc[df['age'].apply(age_18to30)].loc[df['score'].apply(level_a)]
#成员写法
#df = df.loc[df.age.apply(age_18to30)].loc[df.score.apply(level_a)] 
#lambda写法
#df = df.loc[df.age.apply(lambda a:18 <= a < 30)].loc[df.score.apply(lambda s:80 <= s <=100)]
print(df)
上一篇 下一篇

猜你喜欢

热点阅读