本文用到的表格內(nèi)容如下:
先來看一下原始情形:
import pandas as pd df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df)
result:
數(shù)學(xué)成績 語文成績 英語成績
0 89 78 98
1 35 34 34
2 43 56 25
3 35 78 83
4 67 46 65
5 89 89 83
6 96 45 83
7 35 67 45
8 35 78 83
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.var())
result:
數(shù)學(xué)成績 語文成績 英語成績
0 35 78 83
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.mode(axis=1))
result:
0 1 2
0 78.0 89.0 98.0
1 34.0 NaN NaN
2 25.0 43.0 56.0
3 35.0 78.0 83.0
4 46.0 65.0 67.0
5 89.0 NaN NaN
6 45.0 83.0 96.0
7 35.0 45.0 67.0
8 35.0 78.0 83.0
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.mode(axis=1))
result:
0 35
dtype: int64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.iloc[[0]].mode())
result:
數(shù)學(xué)成績 語文成績 英語成績
0 89 78 98
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df[['數(shù)學(xué)成績', "語文成績"]].mode())
result:
數(shù)學(xué)成績 語文成績
0 35 78
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.iloc[[0, 1]].mode())
result:
數(shù)學(xué)成績 語文成績 英語成績
0 35 34 34
1 89 78 98
分位數(shù)是比中位數(shù)更加詳細(xì)的基于位置的指標(biāo),分位數(shù)主要有四分之一分位數(shù),二分之一分位數(shù)(就是中位數(shù))、四分之三分位數(shù)
import pandas as pd df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.quantile(0.25))
result:
數(shù)學(xué)成績 35.0
語文成績 46.0
英語成績 45.0
Name: 0.25, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.quantile(0.75))
result:
數(shù)學(xué)成績 89.0
語文成績 78.0
英語成績 83.0
Name: 0.75, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.quantile(0.25))
result:
數(shù)學(xué)成績 35.0
語文成績 46.0
英語成績 45.0
Name: 0.25, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.quantile(0.25, axis=1))
result:
0 83.5
1 34.0
2 34.0
3 56.5
4 55.5
5 86.0
6 64.0
7 40.0
8 56.5
Name: 0.25, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df['數(shù)學(xué)成績'].quantile(0.25))
result:
35.0
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.iloc[[0]].quantile(0.25))
result:
數(shù)學(xué)成績 89.0
語文成績 78.0
英語成績 98.0
Name: 0.25, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df[['數(shù)學(xué)成績', "語文成績"]].quantile(0.25))
result:
數(shù)學(xué)成績 35.0
語文成績 46.0
Name: 0.25, dtype: float64
df = pd.read_excel(r'C:\Users\admin\Desktop\測試.xlsx') print(df.iloc[[0, 1]].quantile(0.25))
result:
數(shù)學(xué)成績 48.5
語文成績 45.0
英語成績 50.0
Name: 0.25, dtype: float64
pandas 和 numpy中都有計(jì)算分位數(shù)的方法,pandas中是quantile,numpy中是percentile
兩個(gè)方法其實(shí)沒什么區(qū)別,用法上稍微不同,quantile的優(yōu)點(diǎn)是與pandas中的groupby結(jié)合使用,可以分組之后取每個(gè)組的某分位數(shù)
quantile代碼:
import pandas as pd import numpy as np data = pd.read_csv('order_rank_p_0409.txt',sep='\t') #將data按id_1 和 id_2 分組 grouped=data.groupby(['id_1','id_2']) #用quantile計(jì)算第40%的分位數(shù) grouped['gmv'].quantile(0.4) #用to_csv生成文件 x.to_csv('order_ran_re.txt',sep= '\t')
percentile代碼:
import pandas as pd import numpy as np data = pd.read_csv('order_rank_p_0409.txt',sep='\t') a = array(data['gmv']) np.percentile(a,0.4)
兩段代碼,兩種方法計(jì)算的結(jié)果是一樣的
到此這篇關(guān)于Python pandas系列之眾數(shù)和分位數(shù)的文章就介紹到這了,更多相關(guān)pandas眾數(shù)和分位數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:雅安 贛州 許昌 渭南 七臺(tái)河 辛集 西安 濰坊
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python pandas入門系列之眾數(shù)和分位數(shù)》,本文關(guān)鍵詞 Python,pandas,入門,系列,之眾,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。