Karoïevski
Karoïevski

Reputation: 15

Labelling a variable according to its quantiles [Python]

I would like to split a column into 4 distinct classes and label each class by 0,1,2,3 according to the 3 quantiles.

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

I then split my column according to its quantiles:

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

Now I would like to have all the values

Thank you very much for your help :)

Upvotes: 1

Views: 589

Answers (1)

not_speshal
not_speshal

Reputation: 23166

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

Upvotes: 2

Related Questions