# pandas数据排序 # series的排序: # Series.sort_values(ascending = True,inplace = False) # 参数说明: # ascending:默认为True升序排序,为False降序排序 # inplace : 是否修改原始的Series # dataFrame排序: # DataFrame.sort_values(by,ascending = True,inplace = False) # 参数说明: # by : 字符串或者list<字符串>,单列排序或者多列排序 # ascending: bool或者list,升序还是降序,如果是list对应by的多列 # inplace : 是否修改原始DataFrame # 0 读取数据 import pandas as pd df = pd.read_csv("beijing_tianqi_2018.csv") # 换掉温度后面的后缀 df.loc[:,"bWendu"] = df["bWendu"].str.replace("℃","").astype("int32") df.loc[:,"yWendu"] = df["yWendu"].str.replace("℃","").astype("int32") # 1 series的排序 df["aqi"].sort_values() df["aqi"].sort_values(ascending = False) df["tianqi"].sort_values() # 2 DataFrame的排序 # 2.1 单列排序 df.sort_values(by = "aqi") df.sort_values(by= "aqi",ascending = False) # 2.2 多列排序 # 按空气质量等级、最高温度排序,默认升序 df.sort_values(by=["aqiLevel","bWendu"]) # 两个字段都降序 df.sort_values(by=["aqiLevel","bWendu"],ascending = False) # 分别指定升序和降序 df.sort_values(by =["aqiLevel","bWendu"],ascending= [False,True])
好好学习,天天向上