如何保存.qcut()的间隔?

时间:2019-06-06 10:11:38

标签: python pandas jupyter cut

有人告诉我使用.qcut()在给定的每个百分位数处查找值。 返回值是相同间隔值的列表。如何将间隔值保存到每个特定的百分位数?

[In] pd.qcut(df['A'], 4)
[Out] 2732426    (35.881, 1363.92]
824448     (35.881, 1363.92]
3450257    (35.881, 1363.92]
1207627    (35.881, 1363.92]
                 ...        
2561280      (-0.001, 4.836]
1377013      (-0.001, 4.836]
3072978      (-0.001, 4.836]
Name: A, Length: 289224, dtype: category
Categories (4, interval[float64]): [(-0.001, 4.836] < (4.836, 14.139] < 
(14.139, 35.881] < (35.881, 1363.92]]

预期结果应为:

Quantiles       25%        50%         75%          100%
Values          4.836      14.139      35.881       1363.92

1 个答案:

答案 0 :(得分:0)

cat.categoriesIntervalIndex.right一起使用,最后将其传递给DataFrame构造函数:

a = pd.qcut(df['A'], 4).cat.categories.right

df1 = pd.DataFrame([a], columns=['25%','50%','75%','100%'])