Skip to content
Advertisement

How to groupby and calculate new field with python pandas?

I’d like to group by a specific column within a data frame called ‘Fruit’ and calculate the percentage of that particular fruit that are ‘Good’

See below for my initial dataframe

JavaScript

Dataframe

JavaScript

See below for my desired output data frame

JavaScript

Note: Because there is 1 “Good” Apple and 1 “Bad” Apple, the percentage of Good Apples is 50%.

See below for my attempt which is overwriting all the columns

JavaScript

See below for resulting table, which seems to calculate percentage but within existing columns instead of new column:

JavaScript

Advertisement

Answer

We can compare Condition with eq and take advantage of the fact that True is (1) and False is (0) when processed as numbers and take the groupby mean over Fruits:

JavaScript

new_df:

JavaScript

We can further map to a format string and rename to get output into the shown desired output:

JavaScript

new_df:

JavaScript

*Naturally further manipulations can be done as well.

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