1# Python, Pandas
2# Sorting dataframe df on the values of a column col1
3
4# Return sorted array without modifying the original one
5df.sort_values(by=["col1"])
6
7# Sort the original array permanently
8df.sort_values(by=["col1"], inplace = True)
1>>> df.sort_values(by=['col1'])
2 col1 col2 col3
30 A 2 0
41 A 1 1
52 B 9 9
65 C 4 3
74 D 7 2
83 NaN 8 4
9
1// Single sort
2>>> df.sort_values(by=['col1'],ascending=False)
3// ascending => [False(reverse order) & True(default)]
4// Multiple Sort
5>>> df.sort_values(by=['col1','col2'],ascending=[True,False])
6// with apply()
7>>> df[['col1','col2']].apply(sorted,axis=1)
8// axis = [1 & 0], 1 = 'columns', 0 = 'index'
1df.rename(columns={1:'month'},inplace=True)
2df['month'] = pd.Categorical(df['month'],categories=['December','November','October','September','August','July','June','May','April','March','February','January'],ordered=True)
3df = df.sort_values('month',ascending=False)