DubbsBrandon
DubbsBrandon

Reputation: 3

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

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.

Upvotes: 0

Views: 47

Answers (1)

user17242583
user17242583

Reputation:

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

Upvotes: 1

Related Questions