Add default value as column if column doesn't exist

Viewed 1989

Suppose that I'm given a pandas DataFrame, and I want to ensure that I have some columns present, and some default values that I can set them to if they're not there. I can do the following:

def add_default_values_and_print(df):
    out_df = df.copy()
    def_values = { 'a':1, 'b':0 }
    for k, v in def_values.iteritems():
        if k not in df:
            out_df[k] = v
    print(out_df.to_csv())

This seems clumsy though, and it feels like there should be a better, more "pandas" way of doing it. If I put the default values in their own frame and do a join, then the first row will have the defaults and the rest will have NaNs:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([[0,1], [2,3]])

In [3]: defaults_df = pd.DataFrame({ 'a':[1], 'b':[0] })

In [4]: df.join(defaults_df, how='outer')
Out[4]:

   0  1    a    b
0  0  1  1.0  0.0
1  2  3  NaN  NaN

This isn't the intended output but I feel like something that works like this should be possible.

Any suggestions?


EDIT: From @Zero 's comments, the following should work:

def add_default_values_and_print(df):
    def_values = { 'a':1, 'b':0 }
    missing_values = {
        k:v for k,v in def_values.iteritems()
        if k not in df
    }
    print(df.assign(**missing_values).to_csv())
1 Answers

Couple of ways using assign

In [3405]: def_values = {'a':1, 'b':0}

In [3406]: df.assign(**{k: def_values[k] for k in def_values if k not in df})
Out[3406]:
   0  1  a  b
0  0  1  1  0
1  2  3  1  0

In [3407]: def_s = pd.Series(def_values)

In [3408]: def_s
Out[3408]:
a      1
b      0
dtype: int64

In [3409]: df.assign(**def_s[~def_s.index.isin(df)])
Out[3409]:
   0  1  a  b
0  0  1  1  0
1  2  3  1  0
Related