-3

I have two datafames df1 and df2. i want to create a output dataframe. The join column between both the dataframes is 'ID'. I am trying Full outer join but looks like it is not supported. I am new to python so any help is appreciated

df1

ID A B C D F
1 2021-05-06 Sam CA Y text
2 2021-05-07 Tom CY text
3 2021-05-08 Jerry MA Y text

df2

ID A B C
1 2021-05-09 Sam CA
2 2021-05-09 Tom CY
3 2021-05-09 Jerry MA
5 2021-05-09 Victor CA
6 2021-05-09 Dick CY
7 2021-05-09 John MA

Output

ID A B C D F
1 2021-05-06 Sam CA Y text
2 2021-05-07 Tom CY text
3 2021-05-08 Jerry MA Y text
5 2021-05-09 Victor CA
6 2021-05-09 Dick CY
7 2021-05-09 John MA

1 Answers1

1

Let's try pandas.DataFrame.append. The documentation is available here.

import pandas as pd
import numpy as np

df1 = pd.DataFrame([['1', '2021-05-06', 'Sam', 'CA', 'Y', 'text'],
                    ['2', '2021-05-07', 'Tom', 'CY', np.nan, 'text'], 
                    ['3', '2021-05-08', 'Jerry', 'MA', 'Y', 'text']], 
                     columns = ['ID', 'A', 'B', 'C', 'D', 'F'])

df2 = pd.DataFrame([['1', '2021-05-09', 'Sam', 'CA'],
                    ['2', '2021-05-09', 'Tom', 'CY'],
                    ['3', '2021-05-09', 'Jerry', 'MA'], 
                    ['4', '2021-05-09', 'Victor', 'CA'], 
                    ['5', '2021-05-09', 'Dick', 'CY'],
                    ['6', '2021-05-09', 'John', 'MA']], 
                     columns = ['ID', 'A', 'B', 'C'])

df1_ID_list = df1['ID'].unique().tolist()

df_result = df1.append(df2.loc[~(df2['ID'].isin(df1_ID_list))], sort = False)

enter image description here