-4

I have the following:

table = [['Country', 'Points'], ['Spain', '7'], ['Spain', '9'], ['Germany', '1'], ['Germany', '3']]

I want to do the mean of Spain (8) and Germany (2) and group them to:

table_result = [['Country', 'Points'], ['Spain', '8'], ['Germany', '2']]
rid
  • 61,078
  • 31
  • 152
  • 193

1 Answers1

0
import pandas as pd

table = [['Country', 'Points'], ['Spain', '7'], ['Spain', '9'], ['Germany', '1'], ['Germany', '3']]
df = pd.DataFrame(table[1:], columns=table[0])

df['Points'] = df['Points'].astype(int)
grouped_df = df.groupby('Country')
mean_df = grouped_df.mean()

Output:

print(mean_df)
         Points
Country        
Germany     2.0
Spain       8.0
chitown88
  • 27,527
  • 4
  • 30
  • 59