我需要在 PySpark Dataframe 中創建一個新列。但是,創建此新列的條件將是動態的
例子:
df = df.withColumn(
'update_date',
to_date(
substring(df['update_date_string'], -8, 8),
'MM-dd-yy',
),
)
要轉換為
column_expression = "to_date(
substring(df['update_date_string'], -8, 8),
'MM-dd-yy',
)"
df = df.withColumn(
'update_date',
expr(column_expression )
)
expr() 的第二個代碼不是創建新列。請建議如何解決這個問題。
uj5u.com熱心網友回復:
在expr()您需要傳遞 SQL 運算式,而不是 python(檔案:https ://sparkbyexamples.com/pyspark/pyspark-sql-expr-expression-function/ )。嘗試
column_expression = "to_date(
substring(update_date_string, -8, 8),
'MM-dd-yy')"
df = df.withColumn(
'update_date',
expr(column_expression )
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/494571.html
