【问题标题】:Merge daily and monthly Timeseries with Pandas使用 Pandas 合并每日和每月时间序列
【发布时间】:2020-05-10 20:36:29
【问题描述】:

我有两个数据框,“bio”和月度数据,我只需要 bio['Bio_Avg'] 列。 还有一个带有每日数据的“tc”。 两者都有日期时间索引。

我需要合并保持每日粒度,但添加重复列 bio['Bio_Avg'] 值的每月数据。

print(bio.head(5))
                        year  month   ncena    ...      contingency  D   Bio_Avg
            Date                              ...                              
            2019-12-01  2019   12       NaN    ...              NaN  1  38826.48
            2019-11-01  2019   11       NaN    ...              NaN  1  33867.68
            2019-10-01  2019   10       NaN    ...              NaN  1  31358.80
            2019-09-01  2019    9       NaN    ...              NaN  1  29583.84
            2019-08-01  2019    8       NaN    ...              NaN  1  27763.12

print(tc.head(5))
                               exchg_rate
            2019-11-01            59.71
            2019-11-02            59.74
            2019-11-03            59.73
            2019-11-04            59.70
            2019-11-05            59.65

预期的结果应该是:

                               exchg_rate   Bio_Avg
            2019-11-01            59.71     33867.68
            2019-11-02            59.74     33867.68
            2019-11-03            59.73     33867.68
            2019-11-04            59.70     33867.68
            2019-11-05            59.65     33867.68

我试过了:

merge = pd.merge(tc, bio, left_index=True, right_index=True)
print(merge.head(5))

我得到:

                               exchg_rate year month  ...      contingency  D   Bio_Avg
            2019-11-01            59.75  2019   11    ...              NaN  1  33867.68
            2019-12-01            59.94  2019   12    ...              NaN  1  38826.48

当然,它只保留与日期完全匹配的日期,但我会忽略两者之间的日子。 在不破坏所有数据帧的情况下,有什么优雅的方法可以解决这个问题?

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    首先我们将重置 df2 上的索引,然后在您的月份列上合并,如果月份列不存在,我们可以使用 df['Date'].dt.month

    #df1 = bio
    
    #df2 = tc
    
    #df2.reset_index(inplace=True)
    df2 = df2.reset_index()
    
    
    merged_df = pd.merge(
        df2,
        df[["month", "Bio_Avg"]],
        left_on=df2['Date'].dt.month,
        right_on="month"
    
    ).drop("month", axis=1)
    

    print(merged_df)
    
            Date  exchg_rate   Bio_Avg
    0 2019-11-01       59.71  33867.68
    1 2019-11-02       59.74  33867.68
    2 2019-11-03       59.73  33867.68
    3 2019-11-04       59.70  33867.68
    4 2019-11-05       59.65  33867.68
    

    编辑:

    使用多个列加入,而不是使用您预先构建的月 + 年列,我们可以从实际的 Date 列中提取它们,使用 pd.to_datetime(your_dfs['Date']) 确保它是日期时间

        final =pd.merge(
        bio[["Bio_Avg", "Date"]],
        tc,
        left_on=[bio["Date"].dt.year, bio["Date"].dt.month],
        right_on=[tc["Date"].dt.year, tc["Date"].dt.month],
        how="right",
        suffixes=("bio", "tc_"),
    ).drop(["key_0", "key_1"], axis=1)
    

    print(final)
    
        Bio_Avg    Datebio    Datetc_  exchg_rate
    0  33867.68 2019-11-01 2019-11-01       59.71
    1  33867.68 2019-11-01 2019-11-02       59.74
    2  33867.68 2019-11-01 2019-11-03       59.73
    3  33867.68 2019-11-01 2019-11-04       59.70
    4  33867.68 2019-11-01 2019-11-05       59.65
    

    【讨论】:

    • 它只使用一年就可以正常工作,但是当你添加多年时,它会带来过去每一年的每个 11 月的值,我怎么能添加第二个条件?月份和年份?
    • @rolldepollo 为延迟表示歉意,因为时间紧迫。为您编辑 = )
    猜你喜欢
    • 2018-06-23
    • 2021-02-13
    • 2022-11-21
    • 2021-05-21
    • 2018-07-15
    • 2020-09-20
    • 2020-03-24
    • 2016-07-18
    • 2018-07-08
    相关资源
    最近更新 更多