Is there a way to check if a column exists in a Pandas DataFrame?
Suppose that I have the following DataFrame:
>>> import pandas as pd >&
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:
get()
DataFrame
df['sum'] = df.get('A', df['B']) + df['C']
The DataFrame get method has similar behavior as python dictionaries.