美文网首页
python和pandas处理excel(7)

python和pandas处理excel(7)

作者: OURPIECE | 来源:发表于2020-09-02 11:49 被阅读0次

筛选和过滤

  • 使用def定义过滤方法
  • 使用.loc[字段.apply(方法名)]
    如下表
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)

相关文章

网友评论

      本文标题:python和pandas处理excel(7)

      本文链接:https://www.haomeiwen.com/subject/qdzmdktx.html