我有一个时间序列数据,其中包含几年来每个月的天数,并尝试创建一个新的数据框,该数据框将以月为行,以年为列。我有这个 DateTime Days Month Year 2004-11-30 3 November 2004 2004-12-31 16 December 2004 2005-01-31 12 January 2005 2005-02-28 11 February 2005 2005-03-31 11 March 2005 ... ... ... ... 2019-06-30 0 June 2019 2019-07-31 2 July 2019 2019-08-31 5 August 2019 2019-09-30 5 September 2019 2019-10-31 3 October 2019我正在努力得到这个Month 2004 2005 ... 2019January nan 12 7February nan 11 9...November 17 17 nanDecember 14 15 nan我创建了一个新的数据框,第一列表示月份,并尝试遍历第一个数据框以将新列(年)和信息添加到单元格,但检查第一个数据框(天)中的月份是否与月份匹配的条件新数据帧中的(输出)永远不会为真,因此新数据帧永远不会更新。我想这是因为以天为单位的月份与同一迭代中输出的月份永远不同。for index, row in days.iterrows():print(days.loc[index, 'Days']) #this prints out as expectedfor month in output.items(): print(index.month_name()) #this prints out as expected if index.month_name()==month: output.at[month, index.year]=days.loc[index, 'Days'] #I wanted to use this to fill up the cells, is this right? print(days.loc[index, 'Days']) #this never gets printed out你能告诉我如何解决这个问题吗?或者也许有更好的方法来完成结果而不是迭代?这是我第一次尝试在 python 中使用库,因此我将不胜感激。
1 回答
ABOUTYOU
TA贡献1812条经验 获得超5个赞
使用pivot, 如果您的输入数据框每月和每年只有一个值:
df.pivot('Month', 'Year', 'Days')
输出:
Year 2004 2005 2019
Month
August NaN NaN 5
December 16 NaN NaN
February NaN 11 NaN
January NaN 12 NaN
July NaN NaN 2
June NaN NaN 0
March NaN 11 NaN
November 3 NaN NaN
October NaN NaN 3
September NaN NaN 5
添加回答
举报
0/150
提交
取消
