亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?pandas?數(shù)據(jù)排序的幾種常用方法

 更新時間:2022年09月14日 16:11:49   作者:soulsoul_god  
這篇文章主要介紹了python?pandas數(shù)據(jù)排序的幾種常用方法,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前言:

pandas中排序的幾種常用方法,主要包括sort_index和sort_values。

基礎數(shù)據(jù):

import pandas as pd
import numpy as np
data = {
    'brand':['Python', 'C', 'C++', 'C#', 'Java'],
    'B':[4,6,8,12,10],
    'A':[10,2,5,20,16],
    'D':[6,18,14,6,12],
    'years':[4,1,1,30,30],
    'C':[8,12,18,8,2]
}
index = [9,3,4,5,2]
df = pd.DataFrame(data=data, index = index)
print("df數(shù)據(jù):\n", df, '\n')

out:

df數(shù)據(jù):
     A   B   C   D   brand  years
9  10   4   8   6  Python      4
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
5  20  12   8   6      C#     30
2  16  10   2  12    Java     30 

按行索引排序:

print("按行索引排序:\n", df.sort_index(), '\n')

out:

按行索引排序:
     A   B   C   D   brand  years
2  16  10   2  12    Java     30
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
5  20  12   8   6      C#     30
9  10   4   8   6  Python      4

通過設置參數(shù)ascending可以設置升序或者降序排序,默認情況下ascending=True,為升序排序。

設置ascending=False時,為降序排序。

print("按行索引降序排序:\n", df.sort_index(ascending=False), '\n')

out:

按行索引降序排序:
     A   B   C   D   brand  years
9  10   4   8   6  Python      4
5  20  12   8   6      C#     30
4   5   8  18  14     C++      1
3   2   6  12  18       C      1
2  16  10   2  12    Java     30

按列的名稱排序:

設置參數(shù)axis=1實現(xiàn)按列的名稱排序:

print("按列名稱排序:\n", df.sort_index(axis=1), '\n')

out:

按列名稱排序:
     A   B   C   D   brand  years
9  10   4   8   6  Python      4
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
5  20  12   8   6      C#     30
2  16  10   2  12    Java     30

同樣,也可以設置ascending參數(shù):

print("按列名稱排序:\n", df.sort_index(axis=1, ascending=False), '\n')

out:

按列名稱排序:
    years   brand   D   C   B   A
9      4  Python   6   8   4  10
3      1       C  18  12   6   2
4      1     C++  14  18   8   5
5     30      C#   6   8  12  20
2     30    Java  12   2  10  16

按數(shù)值排序:

sort_values()是pandas中按數(shù)值排序的函數(shù):

1、按單個列的值排序

sort_values()中設置單個列的列名,可以對單個列進行排序,通過設置ascending可以設置升序或者降序。

print("按列名稱A排序:\n", df.sort_values('A'), '\n')

out:

按列名稱排序:
     A   B   C   D   brand  years
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
9  10   4   8   6  Python      4
2  16  10   2  12    Java     30
5  20  12   8   6      C#     30

設置ascending=False進行降序排序:

print("按列名稱A降序排序:\n", df.sort_values('A', ascending=False), '\n')

out:

按列名稱A降序排序:
     A   B   C   D   brand  years
5  20  12   8   6      C#     30
2  16  10   2  12    Java     30
9  10   4   8   6  Python      4
4   5   8  18  14     C++      1
3   2   6  12  18       C      1

按多個列的值排序:

先按year列的數(shù)據(jù)進行升序排序,year列相同的再看B列進行升序排序

print("按多個列排序:\n", df.sort_values(['years', 'B']), '\n')

out:

按多個列排序:
     A   B   C   D   brand  years
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
9  10   4   8   6  Python      4
2  16  10   2  12    Java     30
5  20  12   8   6      C#     30 

也可以分別設置列的升序、降序來排序:

years列為升序,B列為降序。

print("按多個列排序:\n", df.sort_values(['years', 'B'], ascending=[True, False]), '\n')

out:

按多個列排序:
     A   B   C   D   brand  years
4   5   8  18  14     C++      1
3   2   6  12  18       C      1
9  10   4   8   6  Python      4
5  20  12   8   6      C#     30
2  16  10   2  12    Java     30

inplace使用:

inplace=True:不創(chuàng)建新的對象,直接對原始對象進行修改;默認是False,即創(chuàng)建新的對象進行修改,原對象不變,和深復制和淺復制有些類似。

df.sort_values('A', inplace=True)
print("按A列排序:\n", df, '\n')

out:

按A列排序:
     A   B   C   D   brand  years
3   2   6  12  18       C      1
4   5   8  18  14     C++      1
9  10   4   8   6  Python      4
2  16  10   2  12    Java     30
5  20  12   8   6      C#     30

缺失值:

含有nan值的數(shù)據(jù)排序:

data = {
    'brand':['Python', 'C', 'C++', 'C#', 'Java'],
    'B':[4,6,8,np.nan,10],
    'A':[10,2,5,20,16],
    'D':[6,18,14,6,12],
    'years':[4,1,1,30,30],
    'C':[8,12,18,8,2]
}
index = [9,3,4,5,2]
df = pd.DataFrame(data=data, index = index)
print("df數(shù)據(jù):\n", df, '\n')

out:

df數(shù)據(jù):
     A     B   C   D   brand  years
9  10   4.0   8   6  Python      4
3   2   6.0  12  18       C      1
4   5   8.0  18  14     C++      1
5  20   NaN   8   6      C#     30
2  16  10.0   2  12    Java     30

B列含有nan值,對B列進行排序,缺失值排在最前面:

print("按B列排序:\n", df.sort_values('B', na_position='first'), '\n')

按B列排序:
     A     B   C   D   brand  years
5  20   NaN   8   6      C#     30
9  10   4.0   8   6  Python      4
3   2   6.0  12  18       C      1
4   5   8.0  18  14     C++      1
2  16  10.0   2  12    Java     30

包含缺失值,缺失值排在最后:

print("按B列排序:\n", df.sort_values('B', na_position='last'), '\n')

out:

按B列排序:
     A     B   C   D   brand  years
9  10   4.0   8   6  Python      4
3   2   6.0  12  18       C      1
4   5   8.0  18  14     C++      1
2  16  10.0   2  12    Java     30
5  20   NaN   8   6      C#     30

到此這篇關于python pandas 數(shù)據(jù)排序的幾種常用方法的文章就介紹到這了,更多相關python pandas內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論