Javatar132
Javatar132

Reputation: 31

Summarizing data using Pandas

I'm trying to filter and summarize a table of data in Python. I'm struggling with selecting the the years (columns that start with 'Y') and calculating the mean for each country in that year. Below is the last bit of code I tried. I'd appreciate any feedback:

My CSV is located at the following link:

https://docs.google.com/spreadsheets/d/1e6R9Tse_Zt3AcY0pPAObdX5XVYKt6ZufHMG7nWLABMQ/edit?usp=sharing

areac= Temp_Change.columns.get_loc('Area')
monthc= Temp_Change.columns.get_loc[8:65]
df = Temp_Change.iloc[areac:monthc]

Upvotes: 1

Views: 33

Answers (1)

W. Ding
W. Ding

Reputation: 689

Please see whether this solves your problem.

dfg = Temp_Change.groupby('Area').mean().loc[:, 'Y1961':]

enter image description here

If you want to stack these year columns.

dfs = dfg.stack().reset_index()
dfs.columns = ['Area', 'Year', 'Avg_Temp']
dfs['Year'] = dfs['Year'].apply(lambda x: int(x[-4:]))

enter image description here

Upvotes: 1

Related Questions