【发布时间】:2018-06-12 23:07:07
【问题描述】:
我在以日期时间值作为索引的数据框旋转时遇到了一些麻烦。 我的 df 看起来像这样:
Timestamp Value
2016-01-01 00:00:00 16.546900
2016-01-01 01:00:00 16.402375
2016-01-01 02:00:00 16.324250
其中时间戳为 a,datetime64[ns]。我正在尝试旋转表格,使其看起来像这样。
Hour 0 1 2 4 ....
Date
2016-01-01 16.5 16.4 16.3 17 ....
....
....
我尝试使用下面的代码,但运行时出现错误。
df3 = pd.pivot_table(df2,index=np.unique(df2.index.date),columns=np.unique(df2.index.hour),values=df2.Temp)
KeyError Traceback (most recent call last)
in ()
1 # Pivot Table
----> 2 df3 = pd.pivot_table(df2,index=np.unique(df2.index.date),columns=np.unique(df2.index.hour),values=df2.Temp)
~\Anaconda3\lib\site-packages\pandas\core\reshape\pivot.py in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
56 for i in values:
57 if i not in data:
---> 58 raise KeyError(i)
59
60 to_filter = []
KeyError: 16.5469
任何帮助或见解将不胜感激。
【问题讨论】:
标签: python pandas dataframe pivot-table