Apache spark 修改spark dataframe列

Apache spark 修改spark dataframe列,apache-spark,spark-dataframe,Apache Spark,Spark Dataframe,我有一个spark数据框,我想添加一个具有特定值的新列。我尝试过使用withcolumn函数,但它没有按预期工作。我想要一个具有特定值的新列,或者我想要替换现有列参见此示例 我有一个数据帧: >>> df.show() +-------+----+-----+---+ | name|year|month|day| +-------+----+-----+---+ | Ali|2014| 9| 1| | Matei|2015| 10| 26| |Micha

我有一个spark数据框,我想添加一个具有特定值的新列。我尝试过使用withcolumn函数,但它没有按预期工作。我想要一个具有特定值的新列,或者我想要替换现有列参见此示例

我有一个数据帧:

>>> df.show()
+-------+----+-----+---+
|   name|year|month|day|
+-------+----+-----+---+
|    Ali|2014|    9|  1|
|  Matei|2015|   10| 26|
|Michael|2015|   10| 25|
|Reynold|2015|   10| 25|
|Patrick|2015|    9|  1|
+-------+----+-----+---+
我想为每一行添加一个信息,我可以使用
lit

>>> from pyspark.sql.functions import lit
>>> df.withColumn('my_new_column', lit('testing info for all')).show()
+-------+----+-----+---+--------------------+
|   name|year|month|day|       my_new_column|
+-------+----+-----+---+--------------------+
|    Ali|2014|    9|  1|testing info for all|
|  Matei|2015|   10| 26|testing info for all|
|Michael|2015|   10| 25|testing info for all|
|Reynold|2015|   10| 25|testing info for all|
|Patrick|2015|    9|  1|testing info for all|
+-------+----+-----+---+--------------------+
如果要为每一行添加不同信息的列表,可以使用
explode

>>> from pyspark.sql.functions import explode
>>> df.withColumn('my_new_column', 
...               explode(array(lit('testing info for all'), 
...                             lit('other testing again')))).show()
+-------+----+-----+---+--------------------+
|   name|year|month|day|       my_new_column|
+-------+----+-----+---+--------------------+
|    Ali|2014|    9|  1|testing info for all|
|    Ali|2014|    9|  1| other testing again|
|  Matei|2015|   10| 26|testing info for all|
|  Matei|2015|   10| 26| other testing again|
|Michael|2015|   10| 25|testing info for all|
|Michael|2015|   10| 25| other testing again|
|Reynold|2015|   10| 25|testing info for all|
|Reynold|2015|   10| 25| other testing again|
|Patrick|2015|    9|  1|testing info for all|
|Patrick|2015|    9|  1| other testing again|
+-------+----+-----+---+--------------------+

请共享示例数据、尝试的代码和预期的输出。我有一些属性id和一些参数。我必须根据参数为属性提供徽章。我已经找到了应该被授予徽章的财产。它存储在数据帧中。现在我必须添加带有一些常量值的列标记。因此,我的输出将类似于带有徽章的属性Id,请举例说明