SoFunction
Updated on 2024-11-18

DataFrame sorting and group sorting in pandas implementation examples

1. sort_values

pandas in the sort_values () function is similar to the principle of SQL in the order by, the data set in accordance with a field in the data for sorting, the function can be based on the specified columns of data can also be based on the specified rows of data sorting.

official document

## Parameters
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')  
#### Parameter Description
axis:{0 or ‘index', 1 or ‘columns'}, default 0,Sort by index by default,i.e. vertical sorting,in case of1,Then it is sorted horizontally    
by:str or list of str;in the event thataxis=0,in that caseby="Listing";in the event thataxis=1,in that caseby="The name of the line.";  
ascending:boolean,Truethen ascending order,It can be[True,False],i.e. first field in ascending order,Second descending order  
inplace:boolean,Whether to replace an existing data frame with a sorted data frame  
kind:Sorting methods,{‘quicksort', ‘mergesort', ‘heapsort'}, default ‘quicksort'。Doesn't seem to matter too much.  
na_position : {‘first', ‘last'}, default ‘last',Default missing values come last  

2. sort_values

Building a DataFrame

import pandas as pd

df = ([['a', 100, 'c'], ['a', 300, 'a'], ['a', 200, 'b'],
                   ['c', 300, 'a'], ['c', 200, 'b'], ['c', 100, 'c'],
                   ['b', 200, 'b'], ['b', 300, 'a'], ['b', 100, 'c']], columns=['X', 'Y', 'Z'])

   X    Y  Z
0  a  100  c
1  a  300  a
2  a  200  b
3  c  300  a
4  c  200  b
5  c  100  c
6  b  200  b
7  b  300  a
8  b  100  c

Sort df by Y, X columns in descending order

df.sort_values(by=['Y', 'X'], ascending=False, inplace=True)
print(df)

   X    Y  Z
3  c  300  a
7  b  300  a
1  a  300  a
4  c  200  b
6  b  200  b
2  a  200  b
5  c  100  c
8  b  100  c
0  a  100  c

3. groupby|sort_values

Sort column Y in ascending order after grouping by column X.

res = ('X', sort=False).apply(lambda x: x.sort_values('Y', ascending=True)).reset_index(drop=True)
print(res)

   X    Y  Z
0  a  100  c
1  a  200  b
2  a  300  a
3  c  100  c
4  c  200  b
5  c  300  a
6  b  100  c
7  b  200  b
8  b  300  a

Example:

Creating Data Frames

#Creating a dataframe using a dictionary dict
import numpy as np
import pandas as pd
df=({'col1':['A','A','B',,'D','C'],
                 'col2':[2,1,9,8,7,7],
                 'col3':[0,1,9,4,2,8]
})
print(df)

>>>
  col1  col2  col3
0    A     2     0
1    A     1     1
2    B     9     9
3  NaN     8     4
4    D     7     2
5    C     7     8

Sort by the first column and place the null value of that column first.

# Sort based on the first column and put the null value of that column first
print(df.sort_values(by=['col1'],na_position='first'))
>>>
  col1  col2  col3
3  NaN     8     4
0    A     2     0
1    A     1     1
2    B     9     9
5    C     7     8
4    D     7     2

Based on the second and third columns, the values are sorted in descending order.

#Based on the second and third columns, values are sorted in descending order
print(df.sort_values(by=['col2','col3'],ascending=False))
>>>
  col1  col2  col3
2    B     9     9
3  NaN     8     4
5    C     7     8
4    D     7     2
0    A     2     0
1    A     1     1

Sort the data according to the values in the first column, in descending order, and replace the original data

# Sort the data according to the values in the first column, in descending order, and replace the original data
df.sort_values(by=['col1'],ascending=False,inplace=True,
                     na_position='first')
print(df)
>>>
  col1  col2  col3
3  NaN     8     4
4    D     7     2
5    C     7     8
2    B     9     9
1    A     1     1
0    A     2     0

Sort the rows with index value 0, i.e., the first row, in descending order.

x = ({'x1':[1,2,2,3],'x2':[4,3,2,1],'x3':[3,2,4,1]}) 
print(x)
# Sort the rows with index value 0, i.e. the first row, in descending order
print(x.sort_values(by =0,ascending=False,axis=1))
>>>
   x1  x2  x3
0   1   4   3
1   2   3   2
2   2   2   4
3   3   1   1
   x2  x3  x1
0   4   3   1
1   3   2   2
2   2   4   2
3   1   1   3

to this article on pandas DataFrame sorting and group sorting of the article is introduced to this, more related pandas sorting and group sorting content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!