[英]Create a Spark dataframe with thousands of columns and then add a column of ArrayType that hold them all
I'd like to create a dataframe in Spark with Scala code like this:我想在 Spark 中使用 Scala 代码创建一个 dataframe ,如下所示:
col_1 ![]() |
col_2 ![]() |
col_3 ![]() |
.. ![]() |
col_2048 ![]() |
---|---|---|---|---|
0.123 ![]() |
0.234 ![]() |
... ![]() |
... ![]() |
0.323 ![]() |
0.345 ![]() |
0.456 ![]() |
... ![]() |
... ![]() |
0.534 ![]() |
Then add an extra column of ArrayType to it, that holds all these 2048 columns data in one column:然后向其中添加一个额外的 ArrayType 列,将所有这些 2048 列数据保存在一列中:
col_1 ![]() |
col_2 ![]() |
col_3 ![]() |
.. ![]() |
col_2048 ![]() |
array_col ![]() |
---|---|---|---|---|---|
0.123 ![]() |
0.234 ![]() |
... ![]() |
... ![]() |
0.323 ![]() |
[0,123, 0.234, ..., 0.323] ![]() |
0.345 ![]() |
0.456 ![]() |
... ![]() |
... ![]() |
0.534 ![]() |
[0.345, 0.456, ..., 0.534] ![]() |
try this尝试这个
df.withColumn("array_col",array(df.columns.map(col): _*)).show
PySpark: PySpark:
Create column list and use python map.创建列列表并使用 python map。
cols = df.columns
df.withColumn('array_col', f.array(*map(lambda c: f.col(c), cols)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.