Pandas: How to get the average of all averages in pandas after groupby

Advertisements

I am working in Pandas and below is the dataframe

# initialize list of lists
data = [['A','Excel','1'], ['A','Word','0'], ['A','Java','1'],['B','Excel','1'],['B','Word','0'],['C','Word','0'],['D','Java','1'],['E','PPT','0'], ['E','Word','0'], ['E','Java','1']]
  
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['System','App','DevTool'])

I obtained the DevTool average for each system using below but how can I get the total average of all averages

df.groupby('System')['DevTool'].mean()*100
System DevTool Ratio
A 66.67
B 50.00
C 00.00
D 100.00
E 33.33

Please advice.

>Solution :

you can use:

# initialize list of lists
data1 = [['A','Excel','1'], ['A','Word','0'], ['A','Java','1'],['B','Excel','1'],['B','Word','0'],['C','Word','0'],['D','Java','1'],['E','PPT','0'], ['E','Word','0'], ['E','Java','1']]

# Create the pandas DataFrame
dfdf = pd.DataFrame(data1, columns=['System','App','DevTool'])

#average  System
dfdf['DevTool'] = dfdf['DevTool'].astype('int')
PVT_T = dfdf.pivot_table(index='System', aggfunc={'DevTool':np.mean})

#average all DevTool
dfdf['DevTool'].mean()

Leave a ReplyCancel reply