Skip to content
Advertisement

Group by calculation pandas

I have a dataframe after applying groupby:

JavaScript

On this, I want to add a new column with the calculation: 10 / (no of items per category). For the example data, this would be:

JavaScript

How can this be done?

Advertisement

Answer

Use Series.value_counts with Series.map:

JavaScript

Or:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement