我有一个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()
答案 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))