适用同样的功能,火花数据框中行的所有领域 [英] Apply same function to all fields of spark dataframe row
本文介绍了适用同样的功能,火花数据框中行的所有领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据帧中,我有大约1000个(变量)列。
I have dataframe in which I have about 1000s ( variable) columns.
我要让所有的值大写。
下面是我想到的方法,你可以建议,如果这是最好的办法。
Here is the approach I have thought of , can you suggest if this is best way.
- 以行
- 找到架构和存储阵列,并找到许多领域如何在那里。
- 通过地图数据帧每一行和高达数组元素的数量限
- 应用功能为大写每个领域和返回的行
推荐答案
如果您只是想同样的功能适用于所有列这样的事情应该是足够的:
If you simply want to apply the same functions to all columns something like this should be enough:
import org.apache.spark.sql.functions.{col, upper}
val df = sc.parallelize(
Seq(("a", "B", "c"), ("D", "e", "F"))).toDF("x", "y", "z")
df.select(df.columns.map(c => upper(col(c)).alias(c)): _*).show
// +---+---+---+
// | x| y| z|
// +---+---+---+
// | A| B| C|
// | D| E| F|
// +---+---+---+
或在Python
from pyspark.sql.functions import col, upper
df = sc.parallelize([("a", "B", "c"), ("D", "e", "F")]).toDF(("x", "y", "z"))
df.select(*(upper(col(c)).alias(c) for c in df.columns)).show()
## +---+---+---+
## | x| y| z|
## +---+---+---+
## | A| B| C|
## | D| E| F|
## +---+---+---+
另请参阅: SparkSQL:将集合函数列名单
这篇关于适用同样的功能,火花数据框中行的所有领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文