柚子快報邀請碼778899分享:pandas 73
柚子快報邀請碼778899分享:pandas 73
73_Pandas獲取分位數(shù)/百分位數(shù)
使用 quantile() 方法獲取 pandas 中 DataFrame 或 Series 的分位數(shù)/百分位數(shù)。
目錄
Quantile() 的基本用法指定要獲取的分位數(shù)/百分位數(shù):參數(shù) q指定interpolation方法:參數(shù)interpolation
數(shù)據(jù)類型 dtype 的差異 指定行/列:參數(shù)axis指定是否處理非數(shù)字值:參數(shù) numeric_only用于字符串上用于日期時間用于布爾值 bool
本文示例代碼的pandas版本如下。請注意,規(guī)格可能因版本而異。以下面的DataFrame為例。
import pandas as pd
print(pd.__version__)
# 1.3.5
df = pd.DataFrame({'col_1': range(11), 'col_2': [i**2 for i in range(11)]})
print(df)
# col_1 col_2
# 0 0 0
# 1 1 1
# 2 2 4
# 3 3 9
# 4 4 16
# 5 5 25
# 6 6 36
# 7 7 49
# 8 8 64
# 9 9 81
# 10 10 100
Quantile() 的基本用法
默認(rèn)情況下,DataFrame 的 quantile() 將每列的中值(1/2 分位數(shù),第 50 個百分位數(shù))返回為 Series。稍后將解釋包含非數(shù)字列的情況。
print(df.quantile())
# col_1 5.0
# col_2 25.0
# Name: 0.5, dtype: float64
print(type(df.quantile()))
#
如果從系列中調(diào)用 quantile(),中值將作為標(biāo)量值返回。
print(df['col_1'].quantile())
# 5.0
print(type(df['col_1'].quantile()))
#
元素類型根據(jù)原始數(shù)據(jù)類型和下述interpolation參數(shù)的設(shè)置而不同。
指定要獲取的分位數(shù)/百分位數(shù):參數(shù) q
指定想要在第一個參數(shù) q 中獲得的 0.0 到 1.0 之間的分位數(shù)/百分比。
print(df.quantile(0.2))
# col_1 2.0
# col_2 4.0
# Name: 0.2, dtype: float64
列表中可以指定多種規(guī)格。在這種情況下,返回值將是一個 DataFrame。
print(df.quantile([0, 0.25, 0.5, 0.75, 1.0]))
# col_1 col_2
# 0.00 0.0 0.0
# 0.25 2.5 6.5
# 0.50 5.0 25.0
# 0.75 7.5 56.5
# 1.00 10.0 100.0
print(type(df.quantile([0, 0.25, 0.5, 0.75, 1.0])))
#
如果指定多個Series,則返回值將為Series。
print(df['col_1'].quantile([0, 0.25, 0.5, 0.75, 1.0]))
# 0.00 0.0
# 0.25 2.5
# 0.50 5.0
# 0.75 7.5
# 1.00 10.0
# Name: col_1, dtype: float64
print(type(df['col_1'].quantile([0, 0.25, 0.5, 0.75, 1.0])))
#
指定interpolation方法:參數(shù) interpolation
值interpolation方法由參數(shù)interpolation指定。默認(rèn)值為“l(fā)inear”.
print(df.quantile(0.21))
# col_1 2.1
# col_2 4.5
# Name: 0.21, dtype: float64
print(df.quantile(0.21, interpolation='linear'))
# col_1 2.1
# col_2 4.5
# Name: 0.21, dtype: float64
“l(fā)ower”使用較小的值,“higher”使用較大的值,“nearest”使用最接近的值。
print(df.quantile(0.21, interpolation='lower'))
# col_1 2
# col_2 4
# Name: 0.21, dtype: int64
print(df.quantile(0.21, interpolation='higher'))
# col_1 3
# col_2 9
# Name: 0.21, dtype: int64
print(df.quantile(0.21, interpolation='nearest'))
# col_1 2
# col_2 4
# Name: 0.21, dtype: int64
“midpoint”是前一個值和后一個值之間的中間值(平均值)。
print(df.quantile(0.21, interpolation='midpoint'))
# col_1 2.5
# col_2 6.5
# Name: 0.21, dtype: float64
數(shù)據(jù)類型 dtype 的差異
默認(rèn)是線性interpolation,因此如果原始數(shù)據(jù)類型dtype是整數(shù)int,則會轉(zhuǎn)換為浮點數(shù)float。請注意,即使該值與原始值相同,數(shù)據(jù)類型也會改變。
print(df.quantile(0.2))
# col_1 2.0
# col_2 4.0
# Name: 0.2, dtype: float64
在“l(fā)ower”、“higher”和“nearest”的情況下,按原樣使用原始值,因此數(shù)據(jù)類型保持不變。
print(df.quantile(0.2, interpolation='lower'))
# col_1 2
# col_2 4
# Name: 0.2, dtype: int64
指定行/列:參數(shù)axis
默認(rèn)是按列處理,但如果 axis 參數(shù)設(shè)置為 1 或 ‘columns’,則會按行處理。
print(df.quantile(axis=1))
# 0 0.0
# 1 1.0
# 2 3.0
# 3 6.0
# 4 10.0
# 5 15.0
# 6 21.0
# 7 28.0
# 8 36.0
# 9 45.0
# 10 55.0
# Name: 0.5, dtype: float64
指定是否處理非數(shù)字值:參數(shù) numeric_only
可以使用參數(shù) numeric_only 指定是否處理非數(shù)字列。將 numeric_only 設(shè)置為 True 將僅定位數(shù)字列,并將其設(shè)置為 False 將定位所有類型的列。 從pandas 2.0開始,numeric_only的默認(rèn)值為False。在此之前確實如此。請注意,這取決于版本。
用于字符串上
以添加了字符串列的 DataFrame 為例。
df_str = df.copy()
df_str['col_3'] = list('abcdefghijk')
print(df_str)
# col_1 col_2 col_3
# 0 0 0 a
# 1 1 1 b
# 2 2 4 c
# 3 3 9 d
# 4 4 16 e
# 5 5 25 f
# 6 6 36 g
# 7 7 49 h
# 8 8 64 i
# 9 9 81 j
# 10 10 100 k
print(df_str.dtypes)
# col_1 int64
# col_2 int64
# col_3 object
# dtype: object
如果參數(shù) numeric_only 設(shè)置為 True,則僅以數(shù)字列為目標(biāo),并且排除字符串列。
print(df_str.quantile(numeric_only=True))
# col_1 5.0
# col_2 25.0
# Name: 0.5, dtype: float64
當(dāng)以參數(shù) numeric_only 設(shè)置為 False(從 pandas 2.0 開始默認(rèn))的字符串列為目標(biāo)時,如果參數(shù)interpolation是“線性”(默認(rèn))或“中點”,則會發(fā)生錯誤。對于“l(fā)ower”、“higher”和“nearest”,該值將是前一個值或根據(jù)字典順序的前一個值。
# print(df_str.quantile())
# TypeError: unsupported operand type(s) for -: 'str' and 'str'
# print(df_str.quantile(interpolation='midpoint'))
# TypeError: unsupported operand type(s) for -: 'str' and 'str'
print(df_str.quantile([0.2, 0.21, 0.3], interpolation='lower'))
# col_1 col_2 col_3
# 0.20 2 4 c
# 0.21 2 4 c
# 0.30 3 9 d
print(df_str.quantile([0.2, 0.21, 0.3], interpolation='higher'))
# col_1 col_2 col_3
# 0.20 2 4 c
# 0.21 3 9 d
# 0.30 3 9 d
print(df_str.quantile([0.2, 0.21, 0.3], interpolation='nearest'))
# col_1 col_2 col_3
# 0.20 2 4 c
# 0.21 2 4 c
# 0.30 3 9 d
用于日期時間
以添加了日期時間列的 DataFrame 為例。
df_dt = df.copy()
df_dt['col_3'] = pd.date_range('2023-01-01', '2023-01-11')
print(df_dt)
# col_1 col_2 col_3
# 0 0 0 2023-01-01
# 1 1 1 2023-01-02
# 2 2 4 2023-01-03
# 3 3 9 2023-01-04
# 4 4 16 2023-01-05
# 5 5 25 2023-01-06
# 6 6 36 2023-01-07
# 7 7 49 2023-01-08
# 8 8 64 2023-01-09
# 9 9 81 2023-01-10
# 10 10 100 2023-01-11
print(df_dt.dtypes)
# col_1 int64
# col_2 int64
# col_3 datetime64[ns]
# dtype: object
如果參數(shù) numeric_only 設(shè)置為 True,則僅將數(shù)字列作為目標(biāo),并且將排除日期和時間列。
print(df_dt.quantile(numeric_only=True))
# col_1 5.0
# col_2 25.0
# Name: 0.5, dtype: float64
即使interpolation參數(shù)是“l(fā)inear”(默認(rèn))或“midpoint”,日期和時間列也會正確interpolation。當(dāng)然,“l(fā)ower”、“higher”和“nearest”也是可以接受的。
print(df_dt.quantile([0.2, 0.21, 0.3]))
# col_1 col_2 col_3
# 0.20 2.0 4.0 2023-01-03 00:00:00
# 0.21 2.1 4.5 2023-01-03 02:24:00
# 0.30 3.0 9.0 2023-01-04 00:00:00
print(df_dt.quantile([0.2, 0.21, 0.3], interpolation='midpoint'))
# col_1 col_2 col_3
# 0.20 2.0 4.0 2023-01-03 00:00:00
# 0.21 2.5 6.5 2023-01-03 12:00:00
# 0.30 3.0 9.0 2023-01-04 00:00:00
print(df_dt.quantile([0.2, 0.21, 0.3], interpolation='lower'))
# col_1 col_2 col_3
# 0.20 2 4 2023-01-03
# 0.21 2 4 2023-01-03
# 0.30 3 9 2023-01-04
print(df_dt.quantile([0.2, 0.21, 0.3], interpolation='higher'))
# col_1 col_2 col_3
# 0.20 2 4 2023-01-03
# 0.21 3 9 2023-01-04
# 0.30 3 9 2023-01-04
print(df_dt.quantile([0.2, 0.21, 0.3], interpolation='nearest'))
# col_1 col_2 col_3
# 0.20 2 4 2023-01-03
# 0.21 2 4 2023-01-03
# 0.30 3 9 2023-01-04
用于布爾值 bool
以添加了一列 boolean 布爾值的 DataFrame 為例。
df_bool = df.copy()
df_bool['col_3'] = [True, False, True, False, True, False, True, False, True, False, True]
print(df_bool)
# col_1 col_2 col_3
# 0 0 0 True
# 1 1 1 False
# 2 2 4 True
# 3 3 9 False
# 4 4 16 True
# 5 5 25 False
# 6 6 36 True
# 7 7 49 False
# 8 8 64 True
# 9 9 81 False
# 10 10 100 True
print(df_bool.dtypes)
# col_1 int64
# col_2 int64
# col_3 bool
# dtype: object
可以使用 select_dtypes() 排除 bool 列,也可以使用 astype() 將其轉(zhuǎn)換為整數(shù) int。
print(df_bool.select_dtypes(exclude=bool))
# col_1 col_2
# 0 0 0
# 1 1 1
# 2 2 4
# 3 3 9
# 4 4 16
# 5 5 25
# 6 6 36
# 7 7 49
# 8 8 64
# 9 9 81
# 10 10 100
print(df_bool.select_dtypes(exclude=bool).quantile())
# col_1 5.0
# col_2 25.0
# Name: 0.5, dtype: float64
print(df_bool.astype({'col_3': int}))
# col_1 col_2 col_3
# 0 0 0 1
# 1 1 1 0
# 2 2 4 1
# 3 3 9 0
# 4 4 16 1
# 5 5 25 0
# 6 6 36 1
# 7 7 49 0
# 8 8 64 1
# 9 9 81 0
# 10 10 100 1
print(df_bool.astype({'col_3': int}).quantile())
# col_1 5.0
# col_2 25.0
# col_3 1.0
# Name: 0.5, dtype: float64
柚子快報邀請碼778899分享:pandas 73
相關(guān)文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。