python - 熊猫合并和填充
问题描述
portfolio_balance_dates_df.merge(historical_transfers_df, left_index=True, right_index=True)
现在我有这个,但它摆脱了两个数据框中不包含的任何行。我想合并并保留所有行并填写不在historical_transfers_df
as中的日期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
... ...
解决方案
这是你要找的吗?使用outer
合并和fillna
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
推荐阅读
- python-3.x - 禁用 Matplotlib 图表的恒定位置重置
- asp.net-core - 级联参数Task是怎么做的
在 Blazor WASM 中的 AuthorizeView 和 AuthorizedRouteView 中解包并公开为“上下文”? - angular - 显示当前用户(Angular 和 Firebase)
- python - 如何在循环中反转字符串
- java - 如何让两个单独的数组彼此相邻显示。一个是普通数组,另一个是 2d
- scheduled-tasks - 使用任务计划程序创建任务以使用命令运行 cmd.exe
- typescript - 如何为扩展基类的类(不是实例)创建类型?
- c++ - 最大数据包数
- wordpress - 替换 Wordpress 标题
- elasticsearch - 比较 Kibana 地图中两个字段的平均值