
3.8.3 Sorting
Pandas also provides operations for sorting the rows in a data frame. The following command can be used to sort our data frame by the values in the ‘m2’ column in decreasing order:
1 2 | dfSorted = df.sort_values(by = 'm2' , ascending = False ) dfSorted |
m1 | m2 | m3 | m4 | m5 | |
---|---|---|---|---|---|
2017-01-05 | 1.200000 | 0.655267 | -1.339799 | 1.075069 | -0.236980 |
2017-01-06 | 0.192804 | 0.192804 | -1.160902 | 0.525051 | -0.412310 |
2017-01-01 | 1.200000 | 0.163613 | 0.510162 | 0.628612 | 0.432523 |
2017-01-02 | 0.056027 | 0.056027 | 0.025050 | 0.283586 | -0.123223 |
2017-01-04 | -0.721431 | -0.721431 | -0.966351 | -0.380911 | 0.001231 |
2017-01-03 | -0.840010 | -0.840010 | -0.422343 | 1.022622 | -0.231232 |
The ‘by’ argument specifies the column that the sorting should be based on and, by setting the ‘ascending’ argument to False, we are saying that we want the rows to be sorted in descending rather than ascending order. It is also possible to provide a list of column names for the ‘by’ argument, to sort by multiple columns. The sort_values(...) method will create a new copy of the data frame, so modifying any cells of dfSorted in this example will not have any impact on the data frame in variable df.