Friday
Friday

Reputation: 11

How do I calculate the mean of one column according to a value in a second column?

I have 2 columns in a DataFrame and I am looking for following solution in Python.

My Dataframe looks currently like this:

columns: INDUSTRY         Revenue
         Service          100
         Manufacturing    50
         Service          200
         Manufacturing    100
         Public           60

What I would like to have is the average per INDUSTRY type in a DataFrame:

columns: INDUSTRY         Revenue
         Service          150
         Manufacturing    75
         Public           60

I know how to do this in R with the function table, but I just started with python. Thank you

Upvotes: 1

Views: 129

Answers (1)

PV8
PV8

Reputation: 6260

In python it is called groupby, as your dataframe is called Industry you have to use:

Industry.groupby('Industry')['Revenue'].mean()

on stackoverflow there are several examples about it: Pandas group-by and sum

Upvotes: 2

Related Questions