熊猫合并和填充

时间:2021-03-10 20:33:38

标签: python pandas dataframe merge fill

portfolio_balance_dates_df.merge(historical_transfers_df, left_index=True, right_index=True)

现在我有了这个,但它删除了两个数据帧中未包含的任何行。我想合并并保留所有行,并将不在 historical_transfers_df 中的日期填写为 0

然后我将使用新合并的数据框减去同一天的余额,以获得没有银行转账的历史余额。

这就是 historical_transfers_df 的样子:

            historical transfers
2021-02-17  202.20
2021-02-14  71.27
2021-02-08  9967.89
...

这就是 portfolio_balance_dates_df 的样子:

            portfolio balance
2020-01-09  4.420000
2020-01-10  4.270000
... ...

1 个答案:

答案 0 :(得分:1)

这是您要找的吗?使用 outer 合并和 fillna with 0:

portfolio_balance_dates_df.merge(historical_transfers_df,
    how='outer',
    left_index=True,
    right_index=True,
).fillna(0)
            portfolio balance   historical transfers
2020-01-09  4.42                0.00
2020-01-10  4.27                0.00
2021-02-08  0.00                9967.89
2021-02-14  0.00                71.27
2021-02-17  0.00                202.20