根据其分位数标记变量 [Python]
Labelling a variable according to its quantiles [Python]
我想将一列拆分为 4 个不同的 classes 并根据 3 个分位数将每个 class 标记为 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
非常感谢您的帮助:)
pd.qcut(df["total_charges"], 4, labels=[1,2,3,4])
我想将一列拆分为 4 个不同的 classes 并根据 3 个分位数将每个 class 标记为 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
非常感谢您的帮助:)
pd.qcut(df["total_charges"], 4, labels=[1,2,3,4])