Check whether a given column is present in a Dataframe
DataFrame is a structure that contains 2-dimensional data and its corresponding labels. DataFrame.columns attribute return the column labels of the given Dataframe . In Order to check if a column exists in Pandas DataFrame, you can use "in" expression.
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Name'] = ['John', 'Doe', 'Bill']
df['Promoted'] = [True, False,True]
df['Marks'] = [82, 38, 63]
df
Name Promoted Marks
0 John True 82
1 Doe False 38
2 Bill True 63
Use in
if 'Promoted' in df.columns:
print("Yes")
else:
print("No")
Yes
Use not in
if 'Promoted' not in df.columns:
print("Yes")
else:
print("No")
No
Check for multiple columns exists in DataFrame
if set(['Name','Promoted']).issubset(df.columns):
print("Yes")
else:
print("No")
Yes

You can alternatively be constructed with curly braces {}.
if not {'Name', 'Promoted'}.issubset(df.columns):
print("Yes")
else:
print("No")
No
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame