Display aggregated values
You may sometimes be required to display your data in a more aggregated form. For example, the co2_levels data contains weekly data, but you may need to display its values aggregated by month of year. In datasets such as the co2_levels DataFrame where the index is a datetime type, you can extract the year of each dates in the index:
# extract of the year in each dates of the df DataFrame
index_year = df.index.year
To extract the month or day of the dates in the indices of the df DataFrame, you would use df.index.month and df.index.day, respectively.
You can then use the extracted year of each indices in the co2_levels DataFrame and the groupby function to compute the mean CO2 levels by year:
df_by_year = df.groupby(index_year).mean()
Este ejercicio forma parte del curso
Visualizing Time Series Data in Python
Instrucciones del ejercicio
- Extract the month for each of the dates in the index of the
co2_levelsDataFrame and assign the values to a variable calledindex_month. - Using the
groupbyandmeanfunctions from thepandaslibrary, compute the monthly mean CO2 levels in theco2_levelsDataFrame and assign that to a new DataFrame calledmean_co2_levels_by_month. - Plot the values of the
mean_co2_levels_by_monthDataFrame using a fontsize of 6 for the axis ticks.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Get month for each dates in the index of co2_levels
index_month = ____.index.____
# Compute the mean CO2 levels for each month of the year
mean_co2_levels_by_month = co2_levels.____(____).____()
# Plot the mean CO2 levels for each month of the year
mean_co2_levels_by_month.____
# Specify the fontsize on the legend
plt.legend(fontsize=10)
# Show plot
plt.show()