Python3 pandas 数据过滤和排序操作

 

筛选

def validate_age(age):
    return 18 <= age <= 29

def validate_score(score,arg):
    return arg <= score



    for sheetname, sheetdatas in studentdatas.items():
        print(sheetdatas.dtypes)
        sheetdatas['Age']=sheetdatas['Age'].astype('int')
        sheetdatas['Score']=sheetdatas['Score'].astype('int')
        print(sheetdatas.dtypes)
        studentdata = sheetdatas.loc[sheetdatas['Age'].apply(validate_age)].loc[sheetdatas['Score'].apply(validate_score,args=(90,))]
        print(studentdata)

 Python3 pandas 数据过滤和排序操作_第1张图片

排序

    for sheetname,sheetdatas in datas.items():
        print(sheetdatas.head(8))
        sheetdatas.sort_values(by=['Worthy','Price'],ascending=[True,False],inplace=True) # False 从大到小
        print(sheetdatas.head(8))

 Python3 pandas 数据过滤和排序操作_第2张图片

 

你可能感兴趣的:(Python)