Python Pandas. Describe() by date

Viewed 746

I would like to plot summary statistics over time for panel data. The X axis would be time and the Y axis would be the variable of interest with lines for Mean, min/max, P25, P50, P75 etc.

This would basically loop through and calc the stats for each date over all the individual observations and then plot them.

What I am trying to do is similar to below, but y axis would be dates instead of 1-10.

import numpy as np
import pandas as pd
# Create random data
rd = pd.DataFrame(np.random.randn(100, 10))
rd.describe().T.drop('count', axis=1).plot()

In my dataset, the time series of each individual is stacked on one another.

I tried running the following but I seem to get the descriptive stats of entire dataset and not broken down by date.

rd = rd.groupby('period').count().describe()
print (rd)
rd.show()

1 Answers

Using the dataframe below as the example:

df = pd.DataFrame({'Values':[10,20,30,20,40,60,40,80,120],'period': [1,2,3,1,2,3,1,2,3]})
df

    Values  period
0   10      1
1   20      2
2   30      3
3   20      1
4   40      2
5   60      3
6   40      1
7   80      2
8   120     3

Now,plotting the descriptive statistics by date using groupby:

df.groupby('period').describe()['Values'].drop('count', axis = 1).plot()
Related