[英]Creating a dataframe with months x years based on time series in pandas
我有一個時間序列數據,其中每個月有幾年的天數,並嘗試創建一個新的 dataframe ,它將月份作為行,將年份作為列。
我有這個
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 ... 2019
January nan 12 7
February nan 11 9
...
November 17 17 nan
December 14 15 nan
我創建了一個新的Z6A8064B5DF47945555555555555555555057DZ,其第一個列表示含義月,並嘗試通過第一個Z6A8064B5DF47945555555555555555555555555555057DZ進行迭代,並將新的列555添加到了Z6A8064B5DF4794555555555555052時,該列是第(YEAR)和信息。在新的 dataframe 中(輸出)永遠不會為真,因此新的 dataframe 永遠不會更新。 我猜這是因為在同一迭代中,天數中的月份與 output 中的月份不同。
for index, row in days.iterrows():
print(days.loc[index, 'Days']) #this prints out as expected
for 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 中使用庫,因此我將不勝感激。
使用pivot
,如果您的輸入 dataframe 每個月和年有一個值:
df.pivot('Month', 'Year', 'Days')
Output:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.