1 | initial version |
To confirm if a Pandas column exists, you can use the in
operator to check if the column name is present in the list of column names:
if column_name in df.columns:
print("Column exists!")
else:
print("Column does not exist.")
Here, df
is the Pandas DataFrame and column_name
is the name of the column that you want to check. df.columns
returns a list of all the column names in the DataFrame. The in
operator checks if column_name
is present in this list, and returns True if it is.