0

I have a large dataframe in which I need to check if a particular column (column_A) exist in dataframe and if the column exist then based on that some processing need to happen otherwise it has to do some other processing -

I am currently trying below -

    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')

There must be a better way of achieving it. Thanks in advance

Codegator
  • 459
  • 7
  • 28

1 Answers1

3

I don't understand what is the "Better" way but this works.

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

# There is id
Lamanus
  • 12,898
  • 4
  • 21
  • 47