我想将数据分组为 4 个范围,我使用 Pandas.cut 进行分箱,这是我的代码和结果
然后我用了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
使用四分位数。只有对于均匀分布的数据,两者才会导致相同的分割。
例子:
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)]
关于python - Pandas .cut VS df.describe(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72725247/