'Multiply each row of one dataframe by all rows of column in second dataframe
I have two dataframes.
Df1
| ID # | Date | Units |
|---|---|---|
| 1821 | 01/01/22 | 300 |
| 1821 | 01/02/22 | 450 |
Df2
| ID # | Hour | % of daily total |
|---|---|---|
| 1821 | 0 | 0.57 |
| 1821 | 1 | 0.89 |
| 1821 | 2 | 1.23 |
| 1821 | 3 | 5.46 |
I'd like to multiply each row in df1 by all rows in df2. Specifically where both ID #'s match.
Final dataframe units represents the units (300 and 450) from df1 multiplied by the % of daily total ( 0.57%, 0.89%, 1.23%, 5.46%) from df2.
| ID # | Date | Hour | Units |
|---|---|---|---|
| 1821 | 01/01/22 | 0 | 2 |
| 1821 | 01/01/22 | 1 | 3 |
| 1821 | 01/01/22 | 2 | 4 |
| 1821 | 01/01/22 | 3 | 16 |
| 1821 | 01/02/22 | 0 | 3 |
| 1821 | 01/02/22 | 1 | 4 |
| 1821 | 01/02/22 | 2 | 6 |
| 1821 | 01/02/22 | 3 | 25 |
Thank you for your help
Solution 1:[1]
Merge the dataframes on ID #, then pop & multiply the column Units and % of daily total, then divide by 100 and round the get the desired result
df3 = df1.merge(df2, on='ID #', how='left')
df3['Units'] = df3.pop('Units').mul(df3.pop('% of daily total'), fill_value=1).div(100).round()
print(df3)
ID # Date Hour Units
0 1821 01/01/22 0 2.0
1 1821 01/01/22 1 3.0
2 1821 01/01/22 2 4.0
3 1821 01/01/22 3 16.0
4 1821 01/02/22 0 3.0
5 1821 01/02/22 1 4.0
6 1821 01/02/22 2 6.0
7 1821 01/02/22 3 25.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shubham Sharma |
