alphanumeric alphanumeric - 1 month ago 12
Python Question

How to assign default value while performing concat on DataFrames

The

result
dataframe gets NaN value assigned to the column
b
which is not presented in dataframe
dfa
:

dfa = pd.DataFrame({'a':[1]})
dfb = pd.DataFrame({'a':[10], 'b':[10]})
result = pd.concat([dfa, dfb])
print result


Here is the result:

a b
0 1 NaN
0 10 10.0


Instead of
NaN
I would rather assign an integer value
0
. Could it be done?

Answer

You can use reindex dfa by columns of dfb and add parameter fill_value=0:

dfa = dfa.reindex(columns=dfb.columns, fill_value=0)
print (dfa)
   a  b
0  1  0

result = pd.concat([dfa, dfb], ignore_index=True)
print (result) 
    a   b
0   1   0
1  10  10