简体   繁体   中英

Convert Timestamp to_datetime() and aggregate by year and month, Pandas, Python

I have a DF, with timestamps that I converted to datetime(), and I want to create a Python table to aggreagate by month and year, but I get the following error:

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()

在此处输入图片说明 Could you please help me?

Try renaming the indices:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM