首页 > 其他 > 详细

Pandas_筛选

时间:2019-05-03 00:16:33      阅读:139      评论:0      收藏:0      [点我收藏+]

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)

结果图:

技术分享图片

 

Pandas_筛选

原文:https://www.cnblogs.com/wodexk/p/10803865.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!