Answers for "sort df python"

5

how to sort in pandas

// Single sort 
>>> df.sort_values(by=['col1'],ascending=False)
// ascending => [False(reverse order) & True(default)]
// Multiple Sort
>>> df.sort_values(by=['col1','col2'],ascending=[True,False])
// with apply() 
>>> df[['col1','col2']].apply(sorted,axis=1)
// axis = [1 & 0], 1 = 'columns', 0 = 'index'
Posted by: Guest on July-06-2020
2

sort by dataframe

DataFrame.sort_values(self, by, axis=0, ascending=True,
                      inplace=False, kind='quicksort',
                      na_position='last',
                      ignore_index=False)

# Example
df.sort_values(by=['ColToSortBy'])
Posted by: Guest on March-13-2020
0

python df pandas sort by column

>>> df.sort_values("city08")
    city08  cylinders fuelType  ...  mpgData            trany  year
99       9          8  Premium  ...        N  Automatic 4-spd  1993
1        9         12  Regular  ...        N     Manual 5-spd  1985
80       9          8  Regular  ...        N  Automatic 3-spd  1985
47       9          8  Regular  ...        N  Automatic 3-spd  1985
3       10          8  Regular  ...        N  Automatic 3-spd  1985
..     ...        ...      ...  ...      ...              ...   ...
9       23          4  Regular  ...        Y  Automatic 4-spd  1993
8       23          4  Regular  ...        Y     Manual 5-spd  1993
7       23          4  Regular  ...        Y  Automatic 3-spd  1993
76      23          4  Regular  ...        Y     Manual 5-spd  1993
2       23          4  Regular  ...        Y     Manual 5-spd  1985
[100 rows x 10 columns]
Posted by: Guest on September-27-2021
-1

sort a dataframe

sort_na_first = gapminder.sort_values('lifeExp',na_position='first')
Posted by: Guest on May-20-2020

Python Answers by Framework

Browse Popular Code Answers by Language