Puneet Shekhawat
Puneet Shekhawat

Reputation: 1777

How to group same element entry and replace it by an average value?

a dataframe is given with city and the supplies, it can contain other columns (which may have different values). Output city name with maximum supplies, in case of multiple entries for any city output city name with the highest average supplies

example:

dataframe

city supplies address columnx columny

A 3000 xyz xyz xyz

B 4000 xyz xyz xyz

C 1000 xyz xyz xyz

A 4000 xyz xyz xyz

D 3000 xyz xyz xyz

B 1000 xyz xyz xyz

then output should be:

city A supplies 3500

use dataframe functions to groupby city and supplies and output city with most average supply.

Upvotes: 0

Views: 42

Answers (1)

Vlad C.
Vlad C.

Reputation: 974

If you use pandas, this would calculate average supplies by city:

dataframe.groupby('city').supplies.mean()

If you want to extract the city with the largest average values for supplies you can do:

dataframe.groupby('city').supplies.mean().idxmax()

Upvotes: 2

Related Questions