'Python SUMIF with one condition across dataframes
I'm working with two dataframes
- MRP:
| Material | Description | Septiembre |
|---|---|---|
| 1208181 | ADSV,NA,MX,ADH HOTMET 814433PM | 630.2888856 |
| 1206500 | SHWP,NA,MX,WRAP M-WRAP 18' | 459.4193011 |
| 3049172 | INSR,LUFTL,BR,LUFTAL | 0 |
| 3049173 | CLOS,LUFTL,BR,BERRY | 0 |
| 3060614 | BOTL,LUFTL,BR,LDPE/HDPE 15 ML | 0 |
- SMCalc:
| Material | Description | Brand | Available Qty | Avail. - 6mth. | sep |
|---|---|---|---|---|---|
| 0324583 | MEAS,MUCNX,US,BLUE,20ML | MCN | 921888 | 980554.96 | |
| 0327757 | CLOS,MUCNX,US,CR24MM | MCN | 9509400 | 6219256.172 | 975724.64 |
| 1019906 | ACETAMINOPHEN DC 90 COARSE L | TEM | 43900 | -4443.531438 | 7372.2407 |
| 1020442 | ACETAMINOPHEN POWDER | NA | 64203.289 | 38020.3542 | 6784.4993 |
| 1120252 | TARTARIC ACID | PIC | 43217.08 | 9370.0843 |
And I'm using this formula in excel: =+SUMIF(MRP!$A:$A,$A2,MRP!C:C) where:
- Range is MRP!A:A (Material)
- Criteria is SMCalc $A2 (Material)
- Sum range is MRP!C:C (Septiembre)
The output I'm looking for is the column F in SMCalc.
Solution 1:[1]
If I'm not wrong, that excel formula calculates the sum of 'Septiembre' in column C of MRP when 'Material' in SMCalc matches 'Material' in MRP...
Assuming you have both excel sheets as pandas dataframes, I would then do:
mrp.groupby('Material')['Septiembre'].sum().reset_index()
To find the sum of 'Septiembre' per material in mrc. Then merge that with the other dataframe:
smcalc.merge(mrp.groupby('Material')['Septiembre'].sum().reset_index(),how='left')
To then bring back those values to smcalc where we want them to be
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 | Daniel Weigel |
