Home > Net >  How do I find the average of a dataframe column when comparing to another column?
How do I find the average of a dataframe column when comparing to another column?

Time:11-25

I can't figure out how to phrase the question well, but basically, let's say I have a dataframe with a column of companies, and another column with the prices they charge for a product. For example:

Company Price
A 50
B 75
A 80
C 10

And I want to find the average like the following:

Company Price
A 65
B 75
C 10

Is there an efficient way to calculate this? I'm sure there is, I just cant think of it.

CodePudding user response:

I think you're looking for this:

df = df.groupby('Company', as_index=False).mean()

Output:

>>> df
  Company  Price
0       A   65.0
1       B   75.0
2       C   10.0
  • Related