如何在Spark Dataframe中的列之间进行一些计算? [英] How to do some calculation between Columns in spark Dataframe?

查看:819
本文介绍了如何在Spark Dataframe中的列之间进行一些计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想添加la和lon列,然后在另一列中输出结果.

For example, I want to plus the la and lon column and output result in another column.

+------+------------------+------------------+
|userid|               la |               lon|
+------+------------------+------------------+
|    u3|               2.0|               2.0|
|    u4|               1.0|               1.0|
|    u5|               2.0|               2.0|
|    u1|1.6666666666666667|2.6666666666666665|
|    u6|               1.0|               3.5|
|    u2|               3.0|               4.0|
+------+------------------+------------------+

推荐答案

如果只需要将两列加起来,那就很简单了:

If you just need to sum two columns together, it's pretty straightforward :

df.withColumn("x", $"la" + $"lon")

x是新列的名称.

将列提升为2的幂:

df.withColumn("x", pow($"la" + $"lon", 2))

这篇关于如何在Spark Dataframe中的列之间进行一些计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆