pandas :具有multiIndex数据框的条形图 [英] Pandas: bar plot with multiIndex dataframe
问题描述
我有一个带有TIMESTAMP
列的pandas DataFrame(不是索引),时间戳格式如下:
I have a pandas DataFrame with a TIMESTAMP
column (not the index), and the timestamp format is as follows:
2015-03-31 22:56:45.510
我也有称为CLASS
和AXLES
的列.我想分别为AXLES
的每个唯一值(AXLES
可以取3-12之间的整数值)分别计算每个月的记录数.
I also have columns called CLASS
and AXLES
. I would like to compute the count of records for each month separately for each unique value of AXLES
(AXLES
can take an integer value between 3-12).
我想出了resample
和groupby
的组合:
resamp = dfWIM.set_index('TIMESTAMP').groupby('AXLES').resample('M', how='count').CLASS
这似乎给了我一个multiIndex数据框对象,如下所示.
This seems to give me a multiIndex dataframe object, as shown below.
In [72]: resamp
Out [72]:
AXLES TIMESTAMP
3 2014-07-31 5517
2014-08-31 31553
2014-09-30 42816
2014-10-31 49308
2014-11-30 44168
2014-12-31 45518
2015-01-31 54782
2015-02-28 52166
2015-03-31 47929
4 2014-07-31 3147
2014-08-31 24810
2014-09-30 39075
2014-10-31 46857
2014-11-30 42651
2014-12-31 48282
2015-01-31 42708
2015-02-28 43904
2015-03-31 50033
在这里,我如何才能访问此multiIndex对象的不同组件,以针对以下条件创建条形图?
From here, how can I access different components of this multiIndex object to create a bar plot for the following conditions?
- 在AXLES = 3时显示数据
- 以月-年"格式显示x刻度(无天,小时,分钟等)
谢谢!
编辑:以下代码为我提供了绘图,但是我无法将xtick格式更改为MM-YY.
EDIT: Following code gives me the plot, but I could not change the xtick formatting to MM-YY.
resamp[3].plot(kind='bar')
EDIT 2 是一个代码片段,可生成与我相似的数据的一小部分样本:
EDIT 2 below is a code snippet that generates a small sample of the data similar to what I have:
dftest = {'TIMESTAMP':['2014-08-31','2014-09-30','2014-10-31'], 'AXLES':[3, 3, 3], 'CLASS':[5,6,7]}
dfTest = pd.DataFrame(dftest)
dfTest.TIMESTAMP = pd.to_datetime(pd.Series(dfTest.TIMESTAMP))
resamp = dfTest.set_index('TIMESTAMP').groupby('AXLES').resample('M', how='count').CLASS
resamp[3].plot(kind='bar')
下面是解决方法:
A.绘制整个重新采样的数据帧(基于@Ako的建议):
A.Plot the whole resampled dataframe (based on @Ako 's suggestion):
df = resamp.unstack(0)
df.index = [ts.strftime('%b 20%y') for ts in df.index]
df.plot(kind='bar', rot=0)
B.从重采样的数据框中绘制单个索引(基于@Alexander的建议):
B.Plot an individual index from the resampled dataframe (based on @Alexander 's suggestion):
df = resamp[3]
df.index = [ts.strftime('%b 20%y') for ts in df.index]
df.plot(kind='bar', rot=0)
推荐答案
以下内容应该可以工作,但是没有一些数据很难进行测试.
The following should work, but it is difficult to test without some data.
首先重置索引以访问TIMESTAMP
列.然后使用strftime
将其格式化为所需的文本表示形式(例如mm-yy).最后,将索引重置为AXLES
和TIMESTAMP
.
Start by resetting your index to get access to the TIMESTAMP
column. Then use strftime
to format it to your desired text representation (e.g. mm-yy). Finally, reset the index back to AXLES
and TIMESTAMP
.
df = resamp.reset_index()
df['TIMESTAMP'] = [ts.strftime('%m-%y') for ts in df.TIMESTAMP]
df.set_index(['AXLES', 'TIMESTAMP'], inplace=True)
>>> df.xs(3, level=0).plot(kind='bar')
这篇关于 pandas :具有multiIndex数据框的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!