Skip to content Skip to sidebar Skip to footer

When Using A Pandas Dataframe, How Do I Add Column If Does Not Exist?

I'm new to using pandas and am writing a script where I read in a dataframe and then do some computation on some of the columns. Sometimes I will have the column called 'Met': df =

Solution 1:

You check it like this:

if 'Met' not in df:
    df['Met'] = df['freqC'] * df['coverage'] 

Solution 2:

If you were creating the dataframe from scratch, you could create the missing columns without a loop merely by passing the column names into the pd.DataFrame() call:

cols = ['column 1','column 2','column 3','column 4','column 5']
df = pd.DataFrame(list_or_dict, index=['a',], columns=cols)

Solution 3:

When interested in conditionally adding columns in a method chain, consider using pipe() with a lambda:

df.pipe(lambda d: (
    d.assign(Met=d['freqC'] * d['coverage'])
    if 'Met' not in d else d
))

Post a Comment for "When Using A Pandas Dataframe, How Do I Add Column If Does Not Exist?"