I have a dataframe:
import pandas as pd
df1 = pd.DataFrame({"Line":['Sales', 'Volume', 'ANR', 'EBT'], "a":[10, 4, 64, 32],
"b":[4, 8, 32, 48],
"c":[16, 4, 8, 24]})
I want to divide each row by a different constant. So I want to divide "Sales" by 10, "Volume" by 1, "ANR" by 100, "EBT" by 1. I can do the following:
df2 = pd.DataFrame({"A":[10, 1, 100, 1]})
df3 = df1.iloc[:, 1:].div(df2.A, axis=0)
This gets me to the right answer for this particular example. But I want the operation to be smarter. I want it to look up the value in the Line column. If it is "Sales", then divide by 10 etc.
The reason is that the dataframe that I am working with has 1000s of rows. I don't want to create and maintain a series like df2.
For the above example, the answer should look like:
df3 = pd.DataFrame({"Line":['Sales', 'Volume', 'ANR', 'EBT'], "a":[1, 4, 0.64, 32],
"b":[0.4, 8, 0.32, 48],
"c":[1.6, 4, 0.8, 24]})