【问题标题】:Replace pyspark column based on other columns根据其他列替换 pyspark 列
【发布时间】:2019-03-21 14:27:42
【问题描述】:

在我的“数据”数据框中,我有 2 列,“time_stamp”和“小时”。我想在缺少 'time_stamp' 值的地方插入 'hour' 列值。我不想创建一个新列,而是在 'time_stamp' 中填充缺失值

我正在尝试将这个 pandas 代码替换为 pyspark 代码:

data['time_stamp'] = data.apply(lambda x: x['hour'] if pd.isna(x['time_stamp']) else x['time_stamp'], axis=1) 

【问题讨论】:

标签: pandas pyspark apache-spark-sql


【解决方案1】:

这样的东西应该可以工作

from pyspark.sql import functions as f

df = (df.withColumn('time_stamp',
 f.expr('case when time_stamp is null then hour else timestamp'))) #added ) which you mistyped

或者,如果您不喜欢 sql:

df = df.withColumn('time_stamp', f.when(f.col('time_stamp').isNull(),f.col('hour'))).otherwise(f.col('timestamp')) # Please correct the Brackets

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多