熊猫合并和填充



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
... ...

这是你要找的吗?使用outerfillna合并为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

最新更新