将时间戳转换为_datetime()并按年和月,熊猫,Python进行汇总

时间:2018-10-01 18:40:17

标签: python pandas datetime dataframe

我有一个DF,带有转换为datetime()的时间戳,我想创建一个Python表按月和年进行汇总,但是出现以下错误:

ValueError: Duplicated level name: "TimeStamp", assigned to level 1, is already used for level 0 


flowData =pd.read_csv('...')

flowData["TimeStamp"] = pd.to_datetime(flowData["TimeStamp"])

pv = flowData.pivot_table(index=flowData['TimeStamp'].dt.month,columns=flowData['TimeStamp'].dt.year, values='Value', aggfunc=np.mean)
pv.head()

enter image description here 你能帮我吗?

1 个答案:

答案 0 :(得分:2)

尝试重命名索引:

pv = (flowData.pivot_table(index=flowData['TimeStamp'].dt.month.rename('month'),
                           columns=flowData['TimeStamp'].dt.year.rename('year'),
                           values='Value', aggfunc=np.mean))