Python 熊猫石斑鱼问题的关键是一个索引
我有一个熊猫数据框,格式如下:Python 熊猫石斑鱼问题的关键是一个索引,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个熊猫数据框,格式如下: Response Time 2018-01-14 00:00:00 201 2018-01-14 00:00:00 400 2018-01-14 00:00:00 200 2018-01-14 00:00:00 400 2018-01-14 00:00:00 200 时间是索引列 我想得到随时间(15分钟间隔)分组的响应图,因此我写了以下内容: for ind, itm in enumerate(df_final['Res
Response
Time
2018-01-14 00:00:00 201
2018-01-14 00:00:00 400
2018-01-14 00:00:00 200
2018-01-14 00:00:00 400
2018-01-14 00:00:00 200
时间是索引列
我想得到随时间(15分钟间隔)分组的响应图,因此我写了以下内容:
for ind, itm in enumerate(df_final['Response'].unique()):
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(key='Time',freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
ax.legend(["Response: {}".format(itm)])
这适用于折旧的TimeGrouper,上面代码中的第二行是:
ax=df_final[df_final['Response'] == item].groupby(pd.TimeGrouper(freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
但是当我运行Grouper代码时,我得到了一个错误:
KeyError: 'The grouper name Time is not found'
我还将键更改为df_final.index.name,但这也导致了KeyError:“找不到grouper名称时间”
索引的类型为index,但我将其更改为DatetimeIndex:
type(df_final.index)
pandas.core.indexes.datetimes.DatetimeIndex
更改索引类型并运行后:
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(key=df_final.index, freq='15Min')).count().plot(kind='bar', figsize=(15,10), title="Response Codes")
我得到:
TypeError: unhashable type: 'DatetimeIndex'
我显然错过了什么。我做错了什么
为了显示索引是什么,df_final.index给出了结果:
DatetimeIndex(['2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
'2018-01-14 00:00:00', '2018-01-14 00:00:00',
...
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00',
'2018-01-15 00:00:00', '2018-01-15 00:00:00'],
dtype='datetime64[ns]', name='Time', length=48960011, freq=None)
在耶斯雷尔的帮助下进行了一些调查后,问题似乎出现在绘图法中。我将代码分解为:
for ind, itm in enumerate(df_final['Response'].unique()):
ax=df_final[df_final['Response'] == itm].groupby(pd.Grouper(level='Time', freq='15Min')).count()
ax.plot(kind='bar', figsize=(15,10), title="Response Codes")
在绘图行中出现的错误为:
~/anaconda2/envs/py3env/lib/python3.6/site-packages/pandas/plotting/_core.py in __init__(self, data, kind, by, subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, fig, title, xlim, ylim, xticks, yticks, sort_columns, fontsize, secondary_y, colormap, table, layout, **kwds)
98 table=False, layout=None, **kwds):
99
--> 100 _converter._WARN = False
101 self.data = data
102 self.by = by
NameError: name '_converter' is not defined
我不知道我是否做错了什么,或者matplotlib中是否有错误,但这是我发现自己被卡住的位置。前一行ax显示了预期的计数和时间我认为您需要:
pd.Grouper(level='Time',freq='15Min')
我相信您可以将列Response
添加到groupby
,通过unstack
重塑形状并打印:
a = df_final.groupby([pd.Grouper(level='Time',freq='15Min'), 'Response'])['Response'].count()
a.unstack().plot(kind='bar', figsize=(15,10), title="Response Codes")
看来问题出在matplotlib版本上。当我回到版本2.0.2时,我没有任何问题。只需使用以下工具卸载matplotlib 2.1.1版:
! pip uninstall -y matplotlib && pip install matplotlib==2.0.2
然后再次导入matplotlib,代码all works会出现错误:NameError:name'\u converter'未定义OK,其他解决方案如何
pd.gropper(level='Time',freq='15Min')
?名称错误:名称'\u converter'未定义您的pandas版本是什么?我假设是最新版本。就在今天早上,我安装了一个小熊猫。您如何找到已安装的版本?