简体   繁体   English

创建一个包含数千列的 Spark dataframe,然后添加一个包含所有列的 ArrayType 列

[英]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_1 col_2 col_2 col_3 col_3 .. .. col_2048 col_2048
0.123 0.123 0.234 0.234 ... ... ... ... 0.323 0.323
0.345 0.345 0.456 0.456 ... ... ... ... 0.534 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_1 col_2 col_2 col_3 col_3 .. .. col_2048 col_2048 array_col array_col
0.123 0.123 0.234 0.234 ... ... ... ... 0.323 0.323 [0,123, 0.234, ..., 0.323] [0,123, 0.234, ..., 0.323]
0.345 0.345 0.456 0.456 ... ... ... ... 0.534 0.534 [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.

相关问题 从Spark数据框列中ArrayType类型的行中获取不同的元素 - Get distinct elements from rows of type ArrayType in Spark dataframe column 有没有办法使用 Crealytics spark-excel package 将带有 ArrayType 列的 Spark dataframe 写入 Excel? - Is there a way to write a Spark dataframe with an ArrayType column to Excel using Crealytics spark-excel package? spark scala:用数千列逐行更新巨大的 dataframe - spark scala: update huge dataframe with thousands of columns row-by-row 如何基于其他列的值在Dataframe中添加列 - How to add column in Dataframe base on the value of other Columns spark Spark Dataframe,使用其他列添加具有功能的新列 - Spark Dataframe, add new column with function using other columns 基于一百列对在Spark dataframe中创建新列 - Create new columns in Spark dataframe based on a hundred column pairs 从 Dataframe 列中提取表情符号并将它们添加到同一 Dataframe Scala Spark 的不同列中 - Extract emojis from Dataframe column and add them into a different Column of the same Dataframe Scala Spark 如何在 dataframe 中创建新列并将它们全部分配为 0? - How to create new columns in a dataframe and assign them all with 0? Spark Dataframe 为所有数字列添加双引号 - Spark Dataframe to add double quotes to all numeric column 在Spark DataFrame中添加一个新列,其中包含一个列的所有值的总和-Scala / Spark - Add a new Column in Spark DataFrame which contains the sum of all values of one column-Scala/Spark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM