python?pandas?數(shù)據(jù)排序的幾種常用方法
前言:
pandas中排序的幾種常用方法,主要包括sort_index和sort_values。
基礎(chǔ)數(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è)置參數(shù)ascending可以設(shè)置升序或者降序排序,默認(rèn)情況下ascending=True,為升序排序。
設(shè)置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è)置參數(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
同樣,也可以設(shè)置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()中設(shè)置單個列的列名,可以對單個列進(jìn)行排序,通過設(shè)置ascending可以設(shè)置升序或者降序。
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
設(shè)置ascending=False進(jìn)行降序排序:
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ù)進(jìn)行升序排序,year列相同的再看B列進(jìn)行升序排序
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
也可以分別設(shè)置列的升序、降序來排序:
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)建新的對象,直接對原始對象進(jìn)行修改;默認(rèn)是False,即創(chuàng)建新的對象進(jìn)行修改,原對象不變,和深復(fù)制和淺復(fù)制有些類似。
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列進(jìn)行排序,缺失值排在最前面:
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
到此這篇關(guān)于python pandas 數(shù)據(jù)排序的幾種常用方法的文章就介紹到這了,更多相關(guān)python pandas內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)分析Pandas?Dataframe排序操作
- Pandas數(shù)值排序 sort_values()的使用
- pandas.DataFrame Series排序的使用(sort_values,sort_index)
- pandas中按行或列的值對數(shù)據(jù)排序的實現(xiàn)
- pandas?dataframe按照列名給列排序三種方法
- Python使用pandas實現(xiàn)對數(shù)據(jù)進(jìn)行特定排序
- Python利用pandas對數(shù)據(jù)進(jìn)行特定排序
- pandas中DataFrame排序及分組排序的實現(xiàn)示例
- pandas中數(shù)據(jù)的排序與排名的具體使用
- Pandas排序和分組排名(sort和rank)的實現(xiàn)
相關(guān)文章
tensorflow 利用expand_dims和squeeze擴(kuò)展和壓縮tensor維度方式
今天小編就為大家分享一篇tensorflow 利用expand_dims和squeeze擴(kuò)展和壓縮tensor維度方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python aiohttp百萬并發(fā)極限測試實例分析
這篇文章主要介紹了Python aiohttp百萬并發(fā)極限測試,結(jié)合實例形式分析了Python異步編程基于aiohttp客戶端高并發(fā)請求的相關(guān)操作技巧與使用注意事項,需要的朋友可以參考下2019-10-10
python項目導(dǎo)入open3d后報錯ImportError:DLL load failed:找不到
這篇文章主要介紹了python項目導(dǎo)入open3d后報錯ImportError:DLL load failed:找不到指定的模塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
使用PyQt的QLabel組件實現(xiàn)選定目標(biāo)框功能的方法示例
這篇文章主要介紹了使用PyQt的QLabel組件實現(xiàn)選定目標(biāo)框功能的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及完美解決
這篇文章主要給大家介紹了關(guān)于Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及解決辦法,pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具,文中通過圖文以及代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
利用Python將每日一句定時推送至微信的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于利用Python將每日一句定時推送至微信的實現(xiàn)方法,文中通過示例代碼將實現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Python中numpy.pad()函數(shù)的使用詳解
這篇文章主要介紹了Python中numpy.pad()函數(shù)的使用詳解,在卷積神經(jīng)網(wǎng)絡(luò)中,為了避免卷積運算導(dǎo)致輸出圖像縮小和圖像邊緣信息丟失,常常采用圖像邊緣填充技術(shù),即在圖像四周邊緣填充0,使得卷積運算后圖像大小不會縮小,同時也不會丟失邊緣和角落的信息,需要的朋友可以參考下2023-10-10

