1

I am using pandas to merge two dataframes. I want to add new columns to existing dataframe one as expected the result below. Please kindly help me to do this.

one:

 Name      Status  Dummy
Kumar     Done       1
Shankar   Progress   1
Shankar   Done       2
sekar      no        0

Two:

Name     Status    Remarks  Temp
Shankar   Progress  Good     5
Shankar    Done     Very     6

Expected Result :

  Name    Status     Dummy    Remarks   Temp
  Kumar     Done       1                 0
  Shankar   Progress   1        Good     5
  Shankar   Done       2        very     6
  sekar      no        0                 0

1 Answer 1

3

Use left join with merge and then replace missing values by fillna:

df = df1.merge(df2, on=['Name','Status'], how='left')
df['Temp'] = df['Temp'].fillna(0).astype(int)
df['Remarks'] = df['Remarks'].fillna('')

print (df)
      Name    Status  Dummy Remarks  Temp
0    Kumar      Done      1             0
1  Shankar  Progress      1    Good     5
2  Shankar      Done      2    Very     6
3    sekar        no      0             0
Sign up to request clarification or add additional context in comments.

4 Comments

Could you plz tell me that how to insert new rows in expected result . i am trying append but its not working for me .df2 = pd.DataFrame([['kishore','progress',1,'Poor',0],['s','Done',2,'good',0]],columns=list(df.columns.values)) df.append(df2)
:( i also tried the same thing which i metioned the above comment
i don't think so
@PythonTeam - i am trying append but its not working for me - can you explain more? Problem with data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.