Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading