pandas操作excel-07-数据筛选

末鹿安然 提交于 2020-02-29 14:22:10
import pandas as pd


def age_18_to_30(a):
    return 18 <= a < 30

def level_a(s):
    return 85 <= s <= 100


students = pd.read_excel('D:/output.xlsx', index_col='idx')

# 筛选出 年龄在18到30之间,成绩在85到100分之间的学员
students = students.loc[students['Age'].apply(age_18_to_30)].loc[students['Score'].apply(level_a)]

# 上一行的写法,也可以这样写
students = students.loc[students.Age.apply(age_18_to_30)].loc[students.Score.apply(level_a)]


# lambda 表达式写法
students = students.loc[students.Age.apply(lambda a : 18 <= a < 30)] \
                   .loc[students.Score.apply(lambda s : 85 <= s <= 100)]

print(students)

#books.to_excel('D:/output.xlsx')

视频链接:https://www.bilibili.com/video/av88814463?p=8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!