635

How do I check if a column exists in a Pandas DataFrame df?

   A   B    C
0  3  40  100
1  6  30  200

How would I check if the column "A" exists in the above DataFrame so that I can compute:

df['sum'] = df['A'] + df['C']

And if "A" doesn't exist:

df['sum'] = df['B'] + df['C']

6 Answers 6

1245

This will work:

if 'A' in df:

But for clarity, I'd probably write it as:

if 'A' in df.columns:
1
  • 1
    df.columns is a list so will it be slower?
    – Ziyuan
    Commented Apr 5, 2023 at 16:32
203

To check if one or more columns all exist, you can use set.issubset, as in:

if set(['A','C']).issubset(df.columns):
   df['sum'] = df['A'] + df['C']                

As @brianpck points out in a comment, set([]) can alternatively be constructed with curly braces,

if {'A', 'C'}.issubset(df.columns):

See this question for a discussion of the curly-braces syntax.

Or, you can use a generator comprehension, as in:

if all(item in df.columns for item in ['A','C']):
0
20

Just to suggest another way without using if statements, you can use the get() method for DataFrames. For performing the sum based on the question:

df['sum'] = df.get('A', df['B']) + df['C']

The DataFrame get method has similar behavior as python dictionaries.

2
  • df.get("A") + df.get("B") still gives you an error if those don't exist, just the more confusing TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' rather than the easier-to-debug KeyError. .get() should only be used if you're actually planning on using the default value, otherwise it just pushes the error away from the point of failure and makes the state contract more confusing to intuit. The whole point of Gerges' answer is to use the second parameter to .get() to specify a column you know will exist as a fallback, not to let a bunch of Nones crash the code.
    – ggorlen
    Commented Nov 11, 2021 at 0:23
  • This is nice because I can check "column exists and is not NaN" with if pandas.notnull(df.get("sum")).
    – Noumenon
    Commented Apr 24, 2023 at 13:19
14

You can also call isin() on the columns to check if specific column(s) exist in it and call any() on the result to reduce it to a single boolean value1. For example, to check if a dataframe contains columns A or C, one could do:

if df.columns.isin(['A', 'C']).any():
    # do something

To check if a column name is not present, you can use the not operator in the if-clause:

if 'A' not in df:
    # do something

or along with the isin().any() call.

if not df.columns.isin(['A', 'C']).any():
    # do something

1: isin() call on the columns returns a boolean array whose values are True if it's either A or C and False otherwise. The truth value of an array is ambiguous, so any() call reduces it to a single True/False value.

6

You can use the set's method issuperset:

set(df).issuperset(['A', 'B'])
# set(df.columns).issuperset(['A', 'B'])
0

yet another new answer to this old question:

if set(('A','B')) <= set(df.columns):
   ...    # 'A' and 'B' are column names of df
else:
   ...    # 'A' or 'B' is not a column name of df

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.