首页 > 解决方案 > 熊猫合并和填充

问题描述

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

现在我有这个,但它摆脱了两个数据框中不包含的任何行。我想合并并保留所有行并填写不在historical_transfers_dfas中的日期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
... ...

标签: pythonpandasdataframemergefill

解决方案


这是你要找的吗?使用outer合并和fillna0:

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

推荐阅读