Python With Excel and Text Strings
Pandas Dataframes
Let’s now group by multiple aggregations at once
Code:
import pandas as pd
df = pd.read_excel('europe_cities_by_sunshine.xlsx')
df = df[['Country', 'Year']]
df = df.groupby('Country').agg(['count','sum']).reset_index()
Explanation:
We can see we’re using the .agg function and passing the count and sum fields to indicate we’d like to count the number of occurrences for each country, along with summing the yearly sunshine as well. The reset_index() just resets the index for an easier way to deal with the data later. We can see Greece has 6 countries in the list, along with an average yearly sunshine of 2910 among those 6 cities.