Loading... <div class="tip inlineBlock error"> ipynb格式:[9.pandas数据排序.html](http://type.zimopy.com/usr/uploads/2022/12/106177135.html) </div> 下面是md格式 # pandas数据排序 # Series的排序: Series.sort_values(ascending = True,inplace = False) 参数说明: - ascending: 默认为True升序排序,为False降序排序 - inplace:是否修改原始Serise # DataFrame的排序: DataFrame.sort_values(by,ascending = True,inplace = False) 参数说明: - by:字符串或者List<字符串>,单列排序或者多列排序 - ascending:bool或者List,升序还是降序,如果是list对应by的多列 - inplace:是否修改原始FataFrame ```python import pandas as pd fpath = "../datas/beijing_tianqi/beijing_tianqi_2018.csv" df=pd.read_csv(fpath) # 替换温度的后缀 df.loc[:,"bWendu"] = df["bWendu"].str.replace("℃","").astype("int32") df.loc[:,"yWendu"] = df["yWendu"].str.replace("℃","").astype("int32") df ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>2018-01-01</td> <td>3</td> <td>-6</td> <td>晴~多云</td> <td>东北风</td> <td>1-2级</td> <td>59</td> <td>良</td> <td>2</td> </tr> <tr> <th>1</th> <td>2018-01-02</td> <td>2</td> <td>-5</td> <td>阴~多云</td> <td>东北风</td> <td>1-2级</td> <td>49</td> <td>优</td> <td>1</td> </tr> <tr> <th>2</th> <td>2018-01-03</td> <td>2</td> <td>-5</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>28</td> <td>优</td> <td>1</td> </tr> <tr> <th>3</th> <td>2018-01-04</td> <td>0</td> <td>-8</td> <td>阴</td> <td>东北风</td> <td>1-2级</td> <td>28</td> <td>优</td> <td>1</td> </tr> <tr> <th>4</th> <td>2018-01-05</td> <td>3</td> <td>-6</td> <td>多云~晴</td> <td>西北风</td> <td>1-2级</td> <td>50</td> <td>优</td> <td>1</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>360</th> <td>2018-12-27</td> <td>-5</td> <td>-12</td> <td>多云~晴</td> <td>西北风</td> <td>3级</td> <td>48</td> <td>优</td> <td>1</td> </tr> <tr> <th>361</th> <td>2018-12-28</td> <td>-3</td> <td>-11</td> <td>晴</td> <td>西北风</td> <td>3级</td> <td>40</td> <td>优</td> <td>1</td> </tr> <tr> <th>362</th> <td>2018-12-29</td> <td>-3</td> <td>-12</td> <td>晴</td> <td>西北风</td> <td>2级</td> <td>29</td> <td>优</td> <td>1</td> </tr> <tr> <th>363</th> <td>2018-12-30</td> <td>-2</td> <td>-11</td> <td>晴~多云</td> <td>东北风</td> <td>1级</td> <td>31</td> <td>优</td> <td>1</td> </tr> <tr> <th>364</th> <td>2018-12-31</td> <td>-2</td> <td>-10</td> <td>多云</td> <td>东北风</td> <td>1级</td> <td>56</td> <td>良</td> <td>2</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> # 1.Series的排序 ```python df["aqi"].sort_values() ``` 271 21 281 21 249 22 272 22 301 22 ... 317 266 71 287 91 287 72 293 86 387 Name: aqi, Length: 365, dtype: int64 ```python df["aqi"].sort_values(ascending=False) ``` 86 387 72 293 71 287 91 287 317 266 ... 249 22 301 22 272 22 271 21 281 21 Name: aqi, Length: 365, dtype: int64 ```python df["tianqi"].sort_values()#会和拼音的排序ASCII有关 ``` 225 中雨~小雨 230 中雨~小雨 197 中雨~雷阵雨 196 中雨~雷阵雨 112 多云 ... 191 雷阵雨~大雨 219 雷阵雨~阴 335 雾~多云 353 霾 348 霾 Name: tianqi, Length: 365, dtype: object # 2.DataFrame 的排序 ## 2.1单列排序 ```python df.sort_values(by="aqi") ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>271</th> <td>2018-09-29</td> <td>22</td> <td>11</td> <td>晴</td> <td>北风</td> <td>3-4级</td> <td>21</td> <td>优</td> <td>1</td> </tr> <tr> <th>281</th> <td>2018-10-09</td> <td>15</td> <td>4</td> <td>多云~晴</td> <td>西北风</td> <td>4-5级</td> <td>21</td> <td>优</td> <td>1</td> </tr> <tr> <th>249</th> <td>2018-09-07</td> <td>27</td> <td>16</td> <td>晴</td> <td>西北风</td> <td>3-4级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>272</th> <td>2018-09-30</td> <td>19</td> <td>13</td> <td>多云</td> <td>西北风</td> <td>4-5级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>301</th> <td>2018-10-29</td> <td>15</td> <td>3</td> <td>晴</td> <td>北风</td> <td>3-4级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>317</th> <td>2018-11-14</td> <td>13</td> <td>5</td> <td>多云</td> <td>南风</td> <td>1-2级</td> <td>266</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>71</th> <td>2018-03-13</td> <td>17</td> <td>5</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>91</th> <td>2018-04-02</td> <td>26</td> <td>11</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>72</th> <td>2018-03-14</td> <td>15</td> <td>6</td> <td>多云~阴</td> <td>东北风</td> <td>1-2级</td> <td>293</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>86</th> <td>2018-03-28</td> <td>25</td> <td>9</td> <td>多云~晴</td> <td>东风</td> <td>1-2级</td> <td>387</td> <td>严重污染</td> <td>6</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> ```python df.sort_values(by="aqi",ascending=False) ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>86</th> <td>2018-03-28</td> <td>25</td> <td>9</td> <td>多云~晴</td> <td>东风</td> <td>1-2级</td> <td>387</td> <td>严重污染</td> <td>6</td> </tr> <tr> <th>72</th> <td>2018-03-14</td> <td>15</td> <td>6</td> <td>多云~阴</td> <td>东北风</td> <td>1-2级</td> <td>293</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>71</th> <td>2018-03-13</td> <td>17</td> <td>5</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>91</th> <td>2018-04-02</td> <td>26</td> <td>11</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>317</th> <td>2018-11-14</td> <td>13</td> <td>5</td> <td>多云</td> <td>南风</td> <td>1-2级</td> <td>266</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>249</th> <td>2018-09-07</td> <td>27</td> <td>16</td> <td>晴</td> <td>西北风</td> <td>3-4级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>301</th> <td>2018-10-29</td> <td>15</td> <td>3</td> <td>晴</td> <td>北风</td> <td>3-4级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>272</th> <td>2018-09-30</td> <td>19</td> <td>13</td> <td>多云</td> <td>西北风</td> <td>4-5级</td> <td>22</td> <td>优</td> <td>1</td> </tr> <tr> <th>271</th> <td>2018-09-29</td> <td>22</td> <td>11</td> <td>晴</td> <td>北风</td> <td>3-4级</td> <td>21</td> <td>优</td> <td>1</td> </tr> <tr> <th>281</th> <td>2018-10-09</td> <td>15</td> <td>4</td> <td>多云~晴</td> <td>西北风</td> <td>4-5级</td> <td>21</td> <td>优</td> <td>1</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> ## 2.2多列排序 ```python # 按空气质量等级。最高温度排序。默认升序 df.sort_values(by=["aqiLevel","bWendu"]) ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>360</th> <td>2018-12-27</td> <td>-5</td> <td>-12</td> <td>多云~晴</td> <td>西北风</td> <td>3级</td> <td>48</td> <td>优</td> <td>1</td> </tr> <tr> <th>22</th> <td>2018-01-23</td> <td>-4</td> <td>-12</td> <td>晴</td> <td>西北风</td> <td>3-4级</td> <td>31</td> <td>优</td> <td>1</td> </tr> <tr> <th>23</th> <td>2018-01-24</td> <td>-4</td> <td>-11</td> <td>晴</td> <td>西南风</td> <td>1-2级</td> <td>34</td> <td>优</td> <td>1</td> </tr> <tr> <th>340</th> <td>2018-12-07</td> <td>-4</td> <td>-10</td> <td>晴</td> <td>西北风</td> <td>3级</td> <td>33</td> <td>优</td> <td>1</td> </tr> <tr> <th>21</th> <td>2018-01-22</td> <td>-3</td> <td>-10</td> <td>小雪~多云</td> <td>东风</td> <td>1-2级</td> <td>47</td> <td>优</td> <td>1</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>71</th> <td>2018-03-13</td> <td>17</td> <td>5</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>90</th> <td>2018-04-01</td> <td>25</td> <td>11</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>218</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>91</th> <td>2018-04-02</td> <td>26</td> <td>11</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>85</th> <td>2018-03-27</td> <td>27</td> <td>11</td> <td>晴</td> <td>南风</td> <td>1-2级</td> <td>243</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>86</th> <td>2018-03-28</td> <td>25</td> <td>9</td> <td>多云~晴</td> <td>东风</td> <td>1-2级</td> <td>387</td> <td>严重污染</td> <td>6</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> ```python #两个字段都是降序 df.sort_values(by=["aqiLevel","bWendu"],ascending=False) ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>86</th> <td>2018-03-28</td> <td>25</td> <td>9</td> <td>多云~晴</td> <td>东风</td> <td>1-2级</td> <td>387</td> <td>严重污染</td> <td>6</td> </tr> <tr> <th>85</th> <td>2018-03-27</td> <td>27</td> <td>11</td> <td>晴</td> <td>南风</td> <td>1-2级</td> <td>243</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>91</th> <td>2018-04-02</td> <td>26</td> <td>11</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>90</th> <td>2018-04-01</td> <td>25</td> <td>11</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>218</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>71</th> <td>2018-03-13</td> <td>17</td> <td>5</td> <td>晴~多云</td> <td>南风</td> <td>1-2级</td> <td>287</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>362</th> <td>2018-12-29</td> <td>-3</td> <td>-12</td> <td>晴</td> <td>西北风</td> <td>2级</td> <td>29</td> <td>优</td> <td>1</td> </tr> <tr> <th>22</th> <td>2018-01-23</td> <td>-4</td> <td>-12</td> <td>晴</td> <td>西北风</td> <td>3-4级</td> <td>31</td> <td>优</td> <td>1</td> </tr> <tr> <th>23</th> <td>2018-01-24</td> <td>-4</td> <td>-11</td> <td>晴</td> <td>西南风</td> <td>1-2级</td> <td>34</td> <td>优</td> <td>1</td> </tr> <tr> <th>340</th> <td>2018-12-07</td> <td>-4</td> <td>-10</td> <td>晴</td> <td>西北风</td> <td>3级</td> <td>33</td> <td>优</td> <td>1</td> </tr> <tr> <th>360</th> <td>2018-12-27</td> <td>-5</td> <td>-12</td> <td>多云~晴</td> <td>西北风</td> <td>3级</td> <td>48</td> <td>优</td> <td>1</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> ```python #分别指定升序和降序 df.sort_values(by=["aqiLevel","bWendu"],ascending=[True,False]) ``` <div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>ymd</th> <th>bWendu</th> <th>yWendu</th> <th>tianqi</th> <th>fengxiang</th> <th>fengli</th> <th>aqi</th> <th>aqiInfo</th> <th>aqiLevel</th> </tr> </thead> <tbody> <tr> <th>178</th> <td>2018-06-28</td> <td>35</td> <td>24</td> <td>多云~晴</td> <td>北风</td> <td>1-2级</td> <td>33</td> <td>优</td> <td>1</td> </tr> <tr> <th>149</th> <td>2018-05-30</td> <td>33</td> <td>18</td> <td>晴</td> <td>西风</td> <td>1-2级</td> <td>46</td> <td>优</td> <td>1</td> </tr> <tr> <th>206</th> <td>2018-07-26</td> <td>33</td> <td>25</td> <td>多云~雷阵雨</td> <td>东北风</td> <td>1-2级</td> <td>40</td> <td>优</td> <td>1</td> </tr> <tr> <th>158</th> <td>2018-06-08</td> <td>32</td> <td>19</td> <td>多云~雷阵雨</td> <td>西南风</td> <td>1-2级</td> <td>43</td> <td>优</td> <td>1</td> </tr> <tr> <th>205</th> <td>2018-07-25</td> <td>32</td> <td>25</td> <td>多云</td> <td>北风</td> <td>1-2级</td> <td>28</td> <td>优</td> <td>1</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>317</th> <td>2018-11-14</td> <td>13</td> <td>5</td> <td>多云</td> <td>南风</td> <td>1-2级</td> <td>266</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>329</th> <td>2018-11-26</td> <td>10</td> <td>0</td> <td>多云</td> <td>东南风</td> <td>1级</td> <td>245</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>335</th> <td>2018-12-02</td> <td>9</td> <td>2</td> <td>雾~多云</td> <td>东北风</td> <td>1级</td> <td>234</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>57</th> <td>2018-02-27</td> <td>7</td> <td>0</td> <td>阴</td> <td>东风</td> <td>1-2级</td> <td>220</td> <td>重度污染</td> <td>5</td> </tr> <tr> <th>86</th> <td>2018-03-28</td> <td>25</td> <td>9</td> <td>多云~晴</td> <td>东风</td> <td>1-2级</td> <td>387</td> <td>严重污染</td> <td>6</td> </tr> </tbody> </table> <p>365 rows × 9 columns</p> </div> 使用数据集也是之前的 最后修改:2022 年 12 月 24 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏