Aporia How to's

How to Check If a Column Exists in a DataFrame?

1 min read
check if column exists in dataframe pandas and pyspark

A DataFrame might contain hundreds of even thousands of columns. It is not possible to visually check if a column exists in such DataFrames.

In this short how-to article, we will learn a practical way of performing this operation in Pandas and PySpark DataFrames.

check-if-column-exists-in-dataframe

Pandas

We can use the in keyword for this task.

"f128" in df.columns
True

It returns True if the given column exists in the DataFrame.

PySpark

The exact same operation works in PySpark as well.

"f128" in df.columns
True

This question is also being asked as:

  • Check for existence of multiple columns

People have also asked for:

Start Monitoring Your Models in Minutes

Book a demo