将时间戳转换为\u datetime()并按年和月、Pandas、Python进行聚合

2024-09-27 19:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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 你能帮帮我吗?在


Tags: tonamedfdatetime错误时间dtlevel
1条回答
网友
1楼 · 发布于 2024-09-27 19:21:16

尝试重命名索引:

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

相关问题 更多 >

    热门问题