我有一個時間序列數據,其中包含幾年來每個月的天數,并嘗試創建一個新的數據框,該數據框將以月為行,以年為列。我有這個 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 中使用庫,因此我將不勝感激。
根據 pandas 中的時間序列創建月 x 年的數據框
慕田峪9158850
2023-02-07 16:29:15