I have a pandas dataframe that incorporates dates, customers, items, and then dollar value for purchases.
date customer product amt
1/1/2017 tim
Let's use product
from itertools, pd.date_range
, and merge
:
from itertools import product
daterange = pd.date_range(df['date'].min(), df['date'].max(), freq='D')
d1 = pd.DataFrame(list(product(daterange,
df['customer'].unique(),
df['product'].unique())),
columns=['date', 'customer', 'product'])
d1.merge(df, on=['date', 'customer', 'product'], how='left').fillna(0)
Output:
date customer product amt
0 2017-01-01 tim apple 3.0
1 2017-01-01 tim melon 0.0
2 2017-01-01 tim orange 0.0
3 2017-01-01 jim apple 0.0
4 2017-01-01 jim melon 2.0
5 2017-01-01 jim orange 0.0
6 2017-01-01 tom apple 5.0
7 2017-01-01 tom melon 4.0
8 2017-01-01 tom orange 0.0
9 2017-01-02 tim apple 0.0
10 2017-01-02 tim melon 0.0
11 2017-01-02 tim orange 0.0
12 2017-01-02 jim apple 0.0
13 2017-01-02 jim melon 0.0
14 2017-01-02 jim orange 0.0
15 2017-01-02 tom apple 0.0
16 2017-01-02 tom melon 0.0
17 2017-01-02 tom orange 0.0
18 2017-01-03 tim apple 0.0
19 2017-01-03 tim melon 0.0
20 2017-01-03 tim orange 0.0
21 2017-01-03 jim apple 0.0
22 2017-01-03 jim melon 0.0
23 2017-01-03 jim orange 0.0
24 2017-01-03 tom apple 0.0
25 2017-01-03 tom melon 0.0
26 2017-01-03 tom orange 0.0
27 2017-01-04 tim apple 0.0
28 2017-01-04 tim melon 3.0
29 2017-01-04 tim orange 0.0
30 2017-01-04 jim apple 2.0
31 2017-01-04 jim melon 0.0
32 2017-01-04 jim orange 0.0
33 2017-01-04 tom apple 0.0
34 2017-01-04 tom melon 1.0
35 2017-01-04 tom orange 4.0