对火花数据框行的所有字段应用相同的函数 [英] 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 中
or in 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|
## +---+---+---+
这篇关于对火花数据框行的所有字段应用相同的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文