【问题标题】:rearrange groups in dataframe based on day and month from another dataframe index根据另一个数据帧索引中的日期和月份重新排列数据帧中的组
【发布时间】:2017-12-04 15:26:28
【问题描述】:

我有 2 个数据框:

df_a

datetime      var
2016-10-15    110.232790
2016-10-16    111.020661
2016-10-17    112.193496
2016-10-18    113.638143
2016-10-19    115.241448
2017-01-01    113.638143
2017-01-02    115.241448

和 df_b

datetime      var
2000-01-01    165.792185
2000-01-02    166.066959
2000-01-03    166.411669
2000-01-04    167.816046
2000-01-05    169.777814
2000-10-15    114.232790
2000-10-16    113.020661
2001-01-01    164.792185
2001-01-02    161.066959
2001-01-03    156.411669
2002-01-04    167.816046
2002-01-05    169.777814
2002-10-15    174.232790
2003-10-16    114.020661

df_a 有 2016 年、2017 年的信息,df_b 有 2000 年到 2015 年的信息(这些年没有重叠)。

我可以将 df_b 数据框中的每个组安排为与 df_a 在一年中的日期方面具有相同的顺序吗?组被定义为具有相同年份的行,例如2000

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以为检查year链接新条件:

    df = df_b[df_b.index.month.isin(df_a.index.month) &
              df_b.index.day.isin(df_a.index.day) & 
              (df_b.index.year == 2000)]
    print (df)
                       var
    datetime              
    2000-01-01  165.792185
    2000-01-02  166.066959
    2000-10-15  114.232790
    2000-10-16  113.020661
    

    编辑:

    df = df_b[df_b.index.month.isin(df_a.index.month) & df_b.index.day.isin(df_a.index.day)]
    print (df)
                       var
    datetime              
    2000-01-01  165.792185
    2000-01-02  166.066959
    2000-10-15  114.232790
    2000-10-16  113.020661
    2001-01-01  164.792185
    2001-01-02  161.066959
    2002-10-15  174.232790
    2003-10-16  114.020661
    
    #create dictionary of weights by factorize
    a = pd.factorize(df_a.index.strftime('%m-%d'))
    d = dict(zip(a[1], a[0]))
    print (d)
    {'01-02': 6, '10-19': 4, '10-18': 3, '10-15': 0, '01-01': 5, '10-16': 1, '10-17': 2}
    
    #ordering Series, multiple by 1000 becasue possible 1 to 366 MMDD
    order = pd.Series(df.index.strftime('%m-%d'), index=df.index).map(d) + df.index.year * 1000
    print (order)
    datetime
    2000-01-01    2000005
    2000-01-02    2000006
    2000-10-15    2000000
    2000-10-16    2000001
    2001-01-01    2001005
    2001-01-02    2001006
    2002-10-15    2002000
    2003-10-16    2003001
    Name: datetime, dtype: int64
    

    最后一个reindexorder 索引排序:

    df = df.reindex(order.sort_values().index)
    print (df)
                       var
    datetime              
    2000-10-15  114.232790
    2000-10-16  113.020661
    2000-01-01  165.792185
    2000-01-02  166.066959
    2001-01-01  164.792185
    2001-01-02  161.066959
    2002-10-15  174.232790
    2003-10-16  114.020661
    

    【讨论】:

    • 感谢@jezrael,但这会打乱顺序。我希望月份和日期顺序遵循 df_a
    • 好的,所以现在我有点困惑,您需要按2000 年或像2001, 2002.. 这样的多年过滤吗?
    • 我想要所有年份 2000、2001 和 2002。这个想法是不要过滤任何年份。只希望 2000、2001、2002 中的每个月和日的顺序 .. 遵循 df_1 中的月和日的顺序
    • 请检查已编辑的答案。解决方案仅在 df_a 中工作 id 唯一的 MMDD 组合。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2020-03-13
    • 2020-09-18
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多