2

I have two dataframe and i need to use 1 as a reference to calculate the other values.

For example, I have df like this:

Brand    LB_ID       Score
BMW      Class       98
BMW      Cost        99
VW       Class       85
VW       Cost        70

And the other df_lb like this

Brand     Total
BMW       56
VW        180

I need to use this formula to calculate another column: (Score(df) / Total(df_lb)) * 100

Normally I can use if-else conditions for such data but I have big data and it will take a lot time to write hundreds of if_else lines... I need an efficient way? Is there any?

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
s_khan92
  • 969
  • 8
  • 21

3 Answers3

3

Use Series.map for new Series by Brand in second DataFrame used for divide by Score column and multiple by 100 for new column:

df['new'] = df['Score'].div(df['Brand'].map(df_lb.set_index('Brand')['Total'])).mul(100)
print (df)
  Brand  LB_ID  Score         new
0   BMW  Class     98  175.000000
1   BMW   Cost     99  176.785714
2    VW  Class     85   47.222222
3    VW   Cost     70   38.888889
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Set Brand as the index for both dataframes and divide :

df["new"] = (df.set_index("Brand")
               .Score
               .div(df_lb.set_index("Brand").Total)
              .mul(100)
              .array)

df


   Brand    LB_ID   Score   new
0   BMW     Class   98  175.000000
1   BMW     Cost    99  176.785714
2   VW      Class   85  47.222222
3   VW      Cost    70  38.888889
sammywemmy
  • 27,093
  • 4
  • 17
  • 31
0

Merge first

m = df.merge(df_lb)

and then compute your new column

m['new'] = 100 * m['Score'] / m['Total']
dmontaner
  • 2,076
  • 1
  • 14
  • 17