【问题标题】:Labelling a variable according to its quantiles [Python]根据分位数标记变量 [Python]
【发布时间】:2021-06-02 01:56:19
【问题描述】:

我想将一列分成 4 个不同的类,并根据 3 个分位数将每个类标记为 0、1、2、3。

This is my Dataframe
     
0      36.88
1      36.88
2      36.88
3      36.88
4      49.12
       ...  
695    80.88
696    30.98
697    31.22
698    31.22
699    31.76
Name: total_charges, Length: 700, dtype: float64

然后我根据分位数拆分我的列:

df['total_charges'].quantile([0.25, 0.5, 0.75])

0.25    28.750
0.50    40.970
0.75    64.335
Name: total_charges, dtype: float64

现在我想拥有所有的价值观

  • 从 0 到 0.25 = 1
  • 0.25 à 50 = 2
  • 0.50 à 0.75 = 3
  • 0.75 à 1 = 4

非常感谢您的帮助:)

【问题讨论】:

    标签: python dataframe quantile


    【解决方案1】:

    pd.qcut(df["total_charges"], 4, labels=[1,2,3,4])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多