![](/img/trans.png)
[英]How to create a new column with a null value using Pyspark DataFrame?
[英]Unable to assign new value to a column in pyspark dataframe using column attribute
我有一個 pyspark dataframe event1
。 它有很多列,其中之一是eventAction
具有分類值,如“轉換”、“簽出”等。
我想以一種方式轉換此列,使eventAction
列中的“轉換”變為 1,其他類別變為 0。
這是我嘗試過的:
event1.eventAction = event1.select(F.when(F.col('eventAction') == 'conversion', 1).otherwise(0))
event1.show()
但是當執行.show()
時,我沒有看到eventAction
列有任何變化。
Spark 數據幀是不可變的,因此您不能直接使用.
符號。 您需要使用withColumn
創建一個新的 dataframe 替換現有列。
import pyspark.sql.functions as F
event1 = event1.withColumn(
'eventAction',
F.when(F.col('eventAction') == 'conversion', 1).otherwise(0)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.