提问者:小点点

基于熊猫时间序列的月×年数据框架的建立


我有一个时间序列数据的天数为每个月为几年,并试图创建一个新的数据框架,将月作为行和年作为列。

我有这个

    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

我创建了一个新的数据框架,第一列表示月份,并尝试在第一个数据框架中迭代以将新列(年)和信息添加到单元格中,但是检查第一个数据框架中的月份(天)是否与新数据框架中的月份(输出)匹配的条件从来不为真,因此新数据框架永远不会更新。 我猜这是因为在同一个迭代中,以天为单位的月份永远不会与以输出为单位的月份相同。

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中的库,因此我将非常感谢您的帮助。


共1个答案

匿名用户

如果您的输入数据帧每月和每年都有一个值,请使用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