jangles
jangles

Reputation: 323

Pandas merge and fill

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

Right now I have this but it gets rid of any row that is not included in both dataframes. I want to merge and keep all of the rows and fill in the dates that are not in the historical_transfers_df as 0.

Then I will use the new merged dataframe to subtract the balance if they are on the same day, to get the historical balance without bank transfers.

This is what historical_transfers_df looks like:

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

This is what portfolio_balance_dates_df looks like:

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

Upvotes: 0

Views: 652

Answers (1)

tdy
tdy

Reputation: 41327

Is this what you're looking for? Use an outer merge and 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

Upvotes: 1

Related Questions