'How to select specific month out of certain data

I want to calculate the monthly average over years, for example, I have monthly data from 2010 to 2020, and now I want to calculate the average for January over this 10 years period. How can I select data for each January throughout these 10 years?



Solution 1:[1]

If you import pandas you can use a DataFrame and groupby with mean of the months:

import pandas as pd

#import data here into dataframe as df

df.groupby(['Month']).mean()

EDIT:

If your data looks something like this:

year,month,data
2010,january,5
2011,january,10
2010,february,15
2011,february,20

you can import the csv and then 'groupby' month and data:

import pandas as pd

#load data
df = pd.read_csv('csv file')

#will show data grouped by months
data_by_month = df.groupby('month').data.mean().reset_index()

#you can filter by the months
data_by_month[data_by_month.month == 'january']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1