【问题标题】:Pyspark : Checking if a column exist in the dataframe [duplicate]Pyspark:检查数据框中是否存在列[重复]
【发布时间】:2020-08-23 01:34:04
【问题描述】:

我有一个大型数据框,我需要检查数据框中是否存在特定列(column_A),如果该列存在,则基于需要进行一些处理,否则它必须进行一些其他处理 -

我目前正在下面尝试 -

    try:
        input_df = input_df.withColumn("column_A", input_df["column_A"].cast(StringType()))
        Do some processing
    except:
        input_df = input_df.drop('column_B')

必须有更好的方法来实现它。提前致谢

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    我不明白什么是“更好”的方式,但这是可行的。

    if "id" in df.columns:
        print("There is id")
    else:
        print("There is no id")
    
    # There is id
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2020-12-31
      • 2022-01-07
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2018-06-19
      相关资源
      最近更新 更多