欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pandas中DataFrame排序及分組排序的實(shí)現(xiàn)示例

 更新時(shí)間:2024年04月03日 11:55:29   作者:craftsman2020  
本文主要介紹了pandas中DataFrame排序及分組排序,pandas中的sort_values()函數(shù)原理類似于SQL中的order by,可以將數(shù)據(jù)集依照某個(gè)字段中的數(shù)據(jù)進(jìn)行排序,下面就來具體介紹一下,感興趣的可以了解一下

1. sort_values

pandas中的sort_values()函數(shù)原理類似于SQL中的order by,可以將數(shù)據(jù)集依照某個(gè)字段中的數(shù)據(jù)進(jìn)行排序,該函數(shù)即可根據(jù)指定列數(shù)據(jù)也可根據(jù)指定行的數(shù)據(jù)排序。

官方文檔

## 參數(shù)    
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')  
#### 參數(shù)說明    
axis:{0 or ‘index', 1 or ‘columns'}, default 0,默認(rèn)按照索引排序,即縱向排序,如果為1,則是橫向排序    
by:str or list of str;如果axis=0,那么by="列名";如果axis=1,那么by="行名";  
ascending:布爾型,True則升序,可以是[True,False],即第一字段升序,第二個(gè)降序  
inplace:布爾型,是否用排序后的數(shù)據(jù)框替換現(xiàn)有的數(shù)據(jù)框  
kind:排序方法,{‘quicksort', ‘mergesort', ‘heapsort'}, default ‘quicksort'。似乎不用太關(guān)心  
na_position : {‘first', ‘last'}, default ‘last',默認(rèn)缺失值排在最后面  

2. 排序sort_values

構(gòu)建DataFrame

import pandas as pd

df = pd.DataFrame([['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

按照Y, X兩列對(duì)df進(jìn)行降序排列

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

按照X列進(jìn)行分組后對(duì)Y列進(jìn)行升序排序

res = df.groupby('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

示例:

創(chuàng)建數(shù)據(jù)框

#利用字典dict創(chuàng)建數(shù)據(jù)框
import numpy as np
import pandas as pd
df=pd.DataFrame({'col1':['A','A','B',np.nan,'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

依據(jù)第一列排序,并將該列空值放在首位

#依據(jù)第一列排序,并將該列空值放在首位
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

依據(jù)第二、三列,數(shù)值降序排序

#依據(jù)第二、三列,數(shù)值降序排序
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

根據(jù)第一列中數(shù)值排序,按降序排列,并替換原數(shù)據(jù)

#根據(jù)第一列中數(shù)值排序,按降序排列,并替換原數(shù)據(jù)
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

按照索引值為0的行,即第一行的值來降序排序

x = pd.DataFrame({'x1':[1,2,2,3],'x2':[4,3,2,1],'x3':[3,2,4,1]}) 
print(x)
#按照索引值為0的行,即第一行的值來降序排序
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

到此這篇關(guān)于pandas中DataFrame排序及分組排序的文章就介紹到這了,更多相關(guān)pandas 排序及分組排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論