1

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

PV8
  • 5,799
  • 7
  • 43
  • 87
Friday
  • 11
  • 2

1 Answers1

2

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

PV8
  • 5,799
  • 7
  • 43
  • 87
  • Thanks, it work. If I want to transform the groupby into a DataFrame: pd.DataFrame(Industry), What happen is that the columns are not on the same level on the first row. The column name Industry starts in row 2. How can I solve that? – Friday Jul 04 '19 at 09:19
  • I am not sure what exactly you mean, but get it back in a dataframe: `df2 = Industry.gorupby('Industry')['Revenue'].mean().reset_index()`should help you – PV8 Jul 04 '19 at 09:37