Lets say I have the following DataFrame:
| A | B | |
|---|---|---|
| 0 | aa | 4.32 |
| 1 | aa | 7.00 |
| 2 | bb | 8.00 |
| 3 | dd | 74.00 |
| 4 | cc | 30.00 |
| 5 | bb | 2.00 |
And let's say I have the following dict which determs the condition for column A in its keys and determs the multiplier for coulmn B in its values:
dict1={'aa':-1, 'bb':2}
All I want is to multiply values in column B with vulues from dict1 based on condition that values in column A are queal to dict1 keys.
So the ouptput should be:
| A | B | |
|---|---|---|
| 0 | aa | -4.32 |
| 1 | aa | -7.00 |
| 2 | bb | 16.00 |
| 3 | dd | 74.00 |
| 4 | cc | 30.00 |
| 5 | bb | 4.00 |
Thanks
CodePudding user response:
Use pd.Series.map:
print (df["A"].map(dict1).fillna(1)*df["B"])
0 -4.32
1 -7.00
2 16.00
3 74.00
4 30.00
5 4.00
dtype: float64
