简体   繁体   中英

Grouping unique values with low value counts

My Data frame contains over 40 unique values for a particular attribute. I want to do some visualisation of this data, but fitting in all 40 points is challenging. Using wine['country'].value_counts() , I can see the frequency of each unique value.

在此处输入图像描述

When I go to create, for example, a bar chart, I would like any unique values with value counts less than 100 to be grouped together to create it's own bar in the visualisation (and say call it 'rest' or 'other').

Any way of doing this?

Initiate a variable x = 0 .Iterate through wine['country'].value_counts() using for loop. Then check if a particular value_counts() is less than 100, if true, then add the value_counts() value for that particular iteration to x . This way you will have the sum of such values whose count is less than 100.

Now before charting, create a new dataframe having data of country vs value_counts() with only those rows whose value_counts() value is greater than 100. Then manually add another row named 'other' to this new dataframe with its value_counts() as x . Use this new dataframe for charting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM