Grouping unique values with low value counts

Stack Overflow Asked by yupper on December 12, 2020

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.

enter image description here

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?

One Answer

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.

Answered by Tarun Khare on December 12, 2020

Add your own answers!

Related Questions

First negative number row in Oracle

3  Asked on November 24, 2021 by user10916118


how to compare hashmap in Java?

2  Asked on November 24, 2021


Java Functional Interfaces and Lambda Expressions

2  Asked on November 24, 2021 by vilitaria


How to fill array in C++?

4  Asked on November 24, 2021 by kuz


Using while to exit for loop

3  Asked on November 24, 2021 by lojcs


How to register SFML Vector2 operators with AngelScript?

2  Asked on November 24, 2021 by veridis_quo_t


How to make a new line in Wrap Widget

2  Asked on November 24, 2021 by hng-trnh


AlarmManager and BroadcastReceiver in Android Oreo+

3  Asked on November 24, 2021 by kelton-holanda-fontenele


HOW? Controller return nothing/current view

5  Asked on November 24, 2021 by dent-argue


PHP get array child

2  Asked on November 24, 2021 by tgezginis


How to enable/disable table column text on click

1  Asked on November 24, 2021 by asif-zaidi


C# Assembly.Load() method loads the wrong dll?

1  Asked on November 24, 2021 by suns-flower


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP