Обзор
Как я могу объединить две панды DataFames, чтобы каждая дата была включена и пустые значения были установлены на ноль? Я внимательно посмотрел на pd.merge_asof , и я не увидел там примера, подходящего для моего варианта использования, и я не могу заставить его работать должным образом.
Код
# imports
import pandas as pd
import numpy as np
# shared data
column_names = ['date', 'gross_profit', 'costs', 'factory_id']
# df1 construction
range_1 = pd.date_range('2019-01-01', periods=3, freq='2D')
gross_profit_1 = [100, 200, 300]
costs_1 = [-20, -30, -40]
factory_id_1 = ['A', 'A', 'A']
values_1 = np.array([range_1, gross_profit_1, costs_1, factory_id_1]).T
df1 = pd.DataFrame(values_1, index=range_1, columns=column_names)
# df2 construction
range_2 = pd.date_range('2019-01-02', periods=3, freq='2D')
gross_profit_2 = [400, -300, 900]
costs_1 = [-90, -80, -70]
factory_id_2 = ['B', 'B', 'B']
values_2 = np.array([range_2, gross_profit_2, costs_2, factory_id_2]).T
df2 = pd.DataFrame(values_2, index=range_2, columns=column_names)
>>> print(df1)
date gross_profit costs factory_id
2019-01-01 2019-01-01 00:00:00 100 -20 A
2019-01-03 2019-01-03 00:00:00 200 -30 A
2019-01-05 2019-01-05 00:00:00 300 -40 A
>>> print(df2)
date gross_profit costs factory_id
2019-01-02 2019-01-02 00:00:00 400 -90 B
2019-01-04 2019-01-04 00:00:00 -300 -80 B
2019-01-06 2019-01-06 00:00:00 900 -70 B
Желаемый merged_df
:
>>> print(merged_df)
date gross_profit_A gross_profit_B
2019-01-01 2019-01-01 00:00:00 100 0
2019-01-02 2019-01-02 00:00:00 0 400
2019-01-03 2019-01-03 00:00:00 200 0
2019-01-04 2019-01-04 00:00:00 0 -300
2019-01-05 2019-01-05 00:00:00 300 0
2019-01-06 2019-01-06 00:00:00 0 900
Пример результирующего расчета:
total_gross_profit = merged_df.gross_profit_A + merged_df.gross_profit_B
cumulative_gross_profit = np.cumsum(total_gross_profit)
>>> print(cumulative_gross_profit)
2019-01-01 100
2019-01-02 500
2019-01-03 700
2019-01-04 400
2019-01-05 700
2019-01-06 1500
Freq: 1D, Name: cumulative_gross_profit, dtype: object
Я включил costs
в каждый DataFrame, потому что я хочу сделать очевидным, что в конечном итоге я хочу сделать это для более чем одного столбца.