0

I have some problem about Pandas.

I have

   df1 = pd.DataFrame({'col1': ['t1' ,'t2','t3','t4','t5'],
            'col2': ['1' ,'2','3','5','6']})

   df2 = pd.DataFrame({'col1': ['t1' ,'t2','t4','t5'],
            'col2': ['5' ,'7','8','2']})

   df3 = pd.DataFrame({'col1': ['t1' ,'t2','t4','t5','t6','t7'],
            'col2': ['6' ,'3','2','5','3','12']})

and result is

   t   d1  d2  d3

   t1   1  5   6
   t2   2  7   3
   t3   3  0   0
   t4   5  8   2
   t5   6  2   5
   t6   0  0   3
   t7   0  0   12

Thanks in advance!

2
  • Just rename the columns and merge (outer) and fill nans with 0 Commented Mar 9, 2022 at 4:47
  • Use df1.merge(df2, on='col1', how='outer').merge(df3, how='outer').fillna(0).rename(columns={'col2_x': 'd1', 'col2_y': 'd2', 'col2': 'd3'}) Commented Mar 9, 2022 at 5:25

1 Answer 1

2

rename the columns and then merge the data frames.

#rename the comlumns
df1.rename(columns={'col2':'d1'},inplace=True)
df2.rename(columns={'col2':'d2'},inplace=True)
df3.rename(columns={'col2':'d3'},inplace=True)

#merge all the dfs; outer merge
from functools import reduce
dfs = [df1, df2, df3]
dfs = reduce(lambda left,right: pd.merge(left,right,how='outer', on='col1'), dfs)

#fill NaN values as 0
dfs.fillna(0,inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.