I am new to pandas and I want to merge two dataframes but I am not sure how to go about doing it properly. So I have a two dataframe df and df1, where df1 is a subset of df. I have performed different operations on df1 and I have new columns and I want to merge it back to df.
df
Index col1 col2 col3
1 a 1234 5a
2 b 3456 c4
3 c 3613 9y
4 d 9103 0l
5 e 8133 9m
6 f 6790 7h
df1
Index col1 col2 col3
3 c 3613 9y
5 e 8133 9m
6 f 6790 7h
I start off with df and df1 and I apply an operation to df1 which results in:
df1
Index col1 col2 col3 col4 col5
3 c 3613 9y qwrt hkjg
5 e 8133 9m asdf jgnm
6 f 6790 7h asdf sajf
So my question now is how do I merge df1 back into df based on col1 to give something like this (essentially adding nans to the columns that were not part of df1 and preserving the order of the original dataframe df)
Index col1 col2 col3 col4 col5
1 a 1234 5a nan nan
2 b 3456 c4 nan nan
3 c 3613 9y qwrt hkjg
4 d 9103 0l nan nan
5 e 8133 9m asdf jgnm
6 f 6790 7h asdf sajf