Wojciech Moszczyński
Wojciech Moszczyński

Reputation: 3187

How to call the quartiles?

I have problem how to call the quartiles. This is csv file: drinksbycountry.csv I make new column where I show the quantile of value from column: 'beer servings'.

df['nowa'] = pd.qcut(df['beer_servings'],6)
df.loc[1:5,['country', 'continent0','nowa' ]]

As the result:

I don't want to have intervals like: (225.0, 376.0]. I would like to show: 'group 1', 'group 2' How to do this?

Upvotes: 1

Views: 49

Answers (1)

jezrael
jezrael

Reputation: 862781

Use parameter labels:

labels : array or boolean, default None
Used as labels for the resulting bins. Must be of the same length as the resulting bins. If False, return only integer indicators of the bins.

df['nowa'] = 'group ' + pd.qcut(df['beer_servings'],6, labels=False).astype(str)

Also is possible create labels in list - e.g. in list comprehension with f-strings:

labels = [f'group {x}' for x in range(6)]
df['nowa'] =  pd.qcut(df['beer_servings'],6, labels=labels)

Upvotes: 3

Related Questions