我有两个数据框,我需要使用1作为参考来计算其他值。
例如,我有df
这样的:
Brand LB_ID Score
BMW Class 98
BMW Cost 99
VW Class 85
VW Cost 70
另一个df_lb
像这样
Brand Total
BMW 56
VW 180
我需要使用此公式来计算另一列: (Score(df) / Total(df_lb)) * 100
通常,我可以对这些数据使用if-else条件,但是我有大数据,编写数百条if_else行将花费大量时间...我需要一种有效的方法吗?有没有?
使用Series.map
新Series
通过Brand
在用于除以第二个数据框Score
柱和多种100
新列:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句