简体   繁体   English

pyspark-如何添加列以从列表中激发 dataframe

[英]pyspark- how to add a column to spark dataframe from a list

I'm looking for a way to add a new column in a Spark DF from a list.我正在寻找一种从列表中在 Spark DF 中添加新列的方法。 In pandas approach it is very easy to deal with it but in spark it seems to be relatively difficult.在 pandas 方法中,处理它很容易,但在 spark 中似乎相对困难。 Please find an examp请找一个例子

#pandas approach
list_example = [1,3,5,7,8]
df.new_column = list_example

#spark ?

Could you please help to resolve this tackle (the easiest possible solution)?你能帮忙解决这个问题吗(最简单的解决方案)?

You could try something like:您可以尝试以下方法:

import pyspark.sql.functions as F

list_example = [1,3,5,7,8]
new_df = df.withColumn("new_column",  F.array( [F.lit(x) for x in list_example] ))
new_df.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM