Consider the following DataFrame:
df:
ticker A B C
date
2022-01-02 145 100 100
2022-01-03 450 200 241
2022-01-04 100 200 NaN
2022-01-05 424 324 222
2022-01-06 400 421 320
I want to multiply the values in the columns prior to the date 2022-01-04
(included) for each column as follows:
For column A, I want to multiply by 2
For column B, I want to multiply by 8
For column C, I want to multiply by 4
The resulting DataFrame should look like this:
df2:
ticker A B C
date
2022-01-02 290 800 400
2022-01-03 900 1600 964
2022-01-04 200 1600 NaN
2022-01-05 424 324 222
2022-01-06 400 421 320
How can this be done?
CodePudding user response:
You could do something like this
df.loc[df["ticker date"]<="2022-01-04", ["A", "B", "C"]] = df.loc[df["ticker date"]<="2022-01-04", ["A", "B", "C"]].apply(lambda x: [x["A"]*2, x["B"]*8, x["C"]*4], axis=1, result_type='broadcast')