簡體   English   中英

Pandas.cut VS df.describe()

[英]Pandas.cut VS df.describe()

我想用 4 個范圍對數據進行分組,我使用 Pandas.cut 進行 bin,這是我的代碼和結果

剪切圖像

然后我使用df.describe()發現邊緣的范圍與 pd.cut 不同,為什么?

描述

pd.cut[(2.719, 3.042] < (3.042, 3.365] < (3.365, 3.688] < (3.688, 4.01]]

df.describe()

min         2.720000
25%         3.110000
50%         3.210000
75%         3.320000
max         4.010000

您的cut將范圍划分為 4 個等寬的bin,而describe使用quartiles 僅對於均勻分布的數據,兩者都會導致相同的細分。

例子:

import pandas as pd
import numpy as np

df = pd.DataFrame({'uniform': np.random.rand(1_000_000), 'normal': np.random.randn(1_000_000)})

with np.printoptions(formatter={'float': '{:.3f}'.format}):
    print( 'uniform:\n'
           f'   {df.uniform.describe().iloc[3:].values}\n'
           f'   {pd.cut(df.uniform, 4).dtype.categories.to_tuples().to_list()}')
    print( 'normal:\n'
           f'   {df.normal.describe().iloc[3:].values}\n'
           f'   {pd.cut(df.normal, 4).dtype.categories.to_tuples().to_list()}')

輸出:

uniform:
   [0.000 0.250 0.499 0.750 1.000]
   [(-0.001, 0.25), (0.25, 0.5), (0.5, 0.75), (0.75, 1.0)]
normal:
   [-4.908 -0.675 0.001 0.674 5.082]
   [(-4.918, -2.411), (-2.411, 0.0867), (0.0867, 2.584), (2.584, 5.082)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM