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())