如何使用pyspark对数据框中的两列进行数学运算 [英] How to do mathematical operation with two column in dataframe using pyspark
本文介绍了如何使用pyspark对数据框中的两列进行数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有带有三列"x","y"和"z"的数据框
I have dataframe with three column "x" ,"y" and "z"
x y z
bn 12452 221
mb 14521 330
pl 12563 160
lo 22516 142
我需要创建由该公式派生的另一列
I need to create a another column which is derived by this formula
(m = z / y+z)
因此,新的数据帧应如下所示:
So the new data frameshould look something like this:
x y z m
bn 12452 221 .01743
mb 14521 330 .02222
pl 12563 160 .01257
lo 22516 142 .00626
推荐答案
df = sqlContext.createDataFrame([('bn', 12452, 221), ('mb', 14521, 330)], ['x', 'y', 'z'])
df = df.withColumn('m', df['z'] / (df['y'] + df['z']))
df.head(2)
这篇关于如何使用pyspark对数据框中的两列进行数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文