008_筛选_过滤

import pandas as pd

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

def level_b(s):
    return 60 <= s < 90


if __name__ == '__main__':
    students = pd.read_excel("C:/Users/123/Desktop/pandas/008_/Students.xlsx", index_col="ID")
    print(students.head)


    # 方法一 : 过滤
    # students = students.loc[students["Age"].apply(validate_age)].loc[students["Score"].apply(level_b)]

    # 方法二 : 过滤
    # students = students.loc[students.Age.apply(validate_age)].loc[students.Score.apply(level_b)]
    # print(students.head)

    # 方法三 : 过滤
    students = students.loc[students.Age.apply(lambda a: 18 <= a < 30)].
        loc[students.Score.apply(lambda s: 60 <= s <= 90)]
    print(students.head)


    # 排序 : 倒序
    students.sort_values(by = "Score", inplace = True, ascending = False)
    print(students.head)
原文地址:https://www.cnblogs.com/huafan/p/14409573.html