从this .csv创建一个数据框,
dfi = pd.read_csv('dfi.csv')
dfi['date'] = pd.to_datetime(dfi['date'])
dfi = dfi.set_index('date')
dfi
Out[181]:
value
date
2019-09-19 a
2019-09-19 a
2019-09-25 b
2019-09-11 a
2019-09-19 a
...
2019-09-19 a
2019-09-11 a
2019-09-19 a
2019-09-16 a
2019-09-11 a
[100 rows x 1 columns]
我可以标出每个值在做的事,
dfi['value'].value_counts().plot.bar()
我也可以通过这样做获得每个期间的计数,
dfi.to_period(myPeriod).groupby('date')['value'].value_counts()
Out[183]:
date value
2019-09-09/2019-09-15 a 32
2019-09-16/2019-09-22 a 49
b 1
d 1
2019-09-23/2019-09-29 a 11
b 3
d 2
c 1
Name: value, dtype: int64
但是我无法设法得出每个时期的图。我的自然猜测是
dfi.to_period(myPeriod).groupby('date')['value'].value_counts().plot.bar()
改为在(时间,值)中滑动类别,
如何在每个期间获得一张直方图?
答案 0 :(得分:2)
您可以unstack
到交叉表中,并在绘图函数中使用subplots
参数,如plot.bar()
的第三个示例所示:
dfi.to_period("W").groupby('date')['value'].value_counts().unstack(0).plot.bar(subplots=True, legend=None, rot=0)