I'm trying to multiply certain columns within a data frame by one specific column. While the specific column will always be the same, the names and number of the other columns will vary so I cannot specify those column names except for what the string they begin with. I have been trying to use .mul but have had trouble getting it to work. Any way I can do this?
Original DF
| ID # | Column To Multiply By | ABC Column1 | ABC Column2 |
|---|---|---|---|
| 12 | 2 | 1 | 2 |
| 13 | 3 | 1 | 2 |
| 14 | 4 | 1 | 2 |
Desired DF
| ID # | Column To Multiply By | ABC Column1 | ABC Column2 |
|---|---|---|---|
| 12 | 2 | 2 | 4 |
| 13 | 3 | 3 | 6 |
| 14 | 4 | 4 | 8 |
I tried using the line below but am met with a syntax error:
df2 = df1.loc[:, df1.columns.str.startswith('ABC').mul(df1.Column to Multiply By, axis=0)