applepie
applepie

Reputation: 25

how to scale columns by column from another Pandas dataframe

I have a Pandas data frame 'df' in which I'd like to perform some scalings column by column.

In column 'A', I need to scale it from the row with id 'A'. In column 'B', I need to scale it from the row with id 'B'. ...

l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]

df = pd.DataFrame([z for z in zip(l1,l2,l3)], columns= ['A', 'B', 'C'])

scaling = pd.DataFrame(dict(id=['A', 'B','C'], scaling = [0.2, 0.3, 0.4]))

What would be the best way to do it?

Upvotes: 0

Views: 155

Answers (2)

sammywemmy
sammywemmy

Reputation: 28644

Assuming the columns are unique, and there are no duplicates in scaling, you could use map:

df.mul(df.columns.map(scaling.set_index("id").scaling))

     A    B    C
0  0.2  1.2  2.8
1  0.4  1.5  3.2
2  0.6  1.8  3.6

Upvotes: 0

charles
charles

Reputation: 1055

How about :

for col in df.columns:
    df[col] = df[col] * scaling.loc[scaling['id'] == col]["scaling"].item()

Upvotes: 1

Related Questions