Pandas_筛选

Students.xlsx表里的数据如下:

 

筛选出年龄在18-30岁,且成绩在85分以上的

import pandas as pd

students = pd.read_excel("../008/Students.xlsx")
students = students.loc[students.Age.apply(lambda a:18<=a<=30)].loc[students.Score.apply(lambda b :b>=85)]
print(students)

结果图:

 

 分解下:

import pandas as pd

students = pd.read_excel("../008/Students.xlsx")
print(students.dtypes)


一、筛出年龄在18-30岁之间的

# 方法1:
def age_18_to_30(a):
#     return 18<=a<=30
    return 18<=a and a<=30

students = students.loc[students["Age"].apply(age_18_to_30)]

# 方法2:
students = students.loc[students.Age.apply(lambda a: 18<=a<=30)]

print(students)

结果图:

 

 二、筛出成绩在85分以上的 

# 方法1:
def score_85(b):
    return b>=85

students = students.loc[students.Score.apply(score_85)]

# 方法2:
students = students.loc[students.Score.apply(lambda b:b>=85)]
print(students)

结果图:

 

posted @ 2019-05-02 23:18  后来的后来123  阅读(803)  评论(0编辑  收藏  举报