pandas: combine two columns in a DataFrame

you can use directly fillna and assigning the result to the column ‘bar’

df['bar'].fillna(df['foo'], inplace=True)
del df['foo']

general example:

import pandas as pd
#creating the table with two missing values
df1 = pd.DataFrame({'a':[1,2],'b':[3,4]}, index = [1,2])
df2 = pd.DataFrame({'b':[5,6]}, index = [3,4])
dftot = pd.concat((df1, df2))
print dftot
#creating the dataframe to fill the missing values
filldf = pd.DataFrame({'a':[7,7,7,7]})

#filling 
print dftot.fillna(filldf)

Leave a Comment