Creating Boxplot using groupby

Viewed 192

I have a df which consists of two columns, 'date' and 'value', containing multiple years data of 'value.' I'd like to create a boxplot of the range of values grouped by month and year. I use groupby to group the values as such:

df.groupby(df['date'].dt.strftime('%b%Y'))['value']

But now I cannot figure out how to operate on the resulting groupby data structure to create the boxplot (via either matplotlib or seaborn). Stumped.

3 Answers

Let's assume your dataframe looks like it was generated by the following code:

import pandas as pd
import numpy as np
df = pd.DataFrame({'date': pd.date_range('2019-1-1', periods=1000, freq='D'), 
                   'value': np.random.randint(0,10,size=1000)})

It looks like this:

          date  value
0   2019-01-01      3
1   2019-01-02      9
2   2019-01-03      2
3   2019-01-04      2
4   2019-01-05      4
..         ...    ...
995 2021-09-22      3
996 2021-09-23      5
997 2021-09-24      1
998 2021-09-25      4
999 2021-09-26      8

Now you can compute a new column month:

df['month'] = df.date.dt.month
          date  value  month
0   2019-01-01      3      1
1   2019-01-02      9      1
2   2019-01-03      2      1
3   2019-01-04      2      1
4   2019-01-05      4      1
..         ...    ...    ...
995 2021-09-22      3      9
996 2021-09-23      5      9
997 2021-09-24      1      9
998 2021-09-25      4      9
999 2021-09-26      8      9

And call boxplot using this new column as grouper:

df.boxplot('value', by='month')

parse_dates your time series Date column and set it as the index. Use pd.Grouper and level 0 to access the index and set the frequency to D or daily. The boxplot will find the min and max frequency and median and outliers based on the grouped.index.year

df=pd.read_csv('sp500.csv',parse_dates=['Date'],index_col='Date')
grouped=df.groupby(pd.Grouper(level=0,freq='D'))['Open'].mean()

sns.boxplot(x=grouped.index.year, y=grouped.values,
data=grouped)
plt.xticks(rotation=90)
plt.show()

You can groupby using the datetime accessor to_period() with a monthly frequency.

import pandas as pd
import numpy as np
df = pd.DataFrame({'date': pd.date_range('2019-1-1', periods=1000, freq='D'), 
                   'value': np.random.randint(0,10,size=1000)})

df.groupby(df.date.dt.to_period("M")).boxplot(subplots=False, rot=90)

enter image description here

Related