0

I have the following dataframes:

df1

enter image description here

and df2

enter image description here

I want to merge the two dataframes at only the uncommon rows and columns such that the outcomes will be like the below as df_merged:

enter image description here

Can anyone suggest the best way to get the result dataframe as shown above?

Tzane
  • 2,752
  • 1
  • 10
  • 21
  • Have you looked through the pandas tag wiki and read the canonical question on merging dataframes? – Paul H Aug 22 '23 at 13:36
  • 1
    Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – PV8 Aug 22 '23 at 13:38

1 Answers1

0

What you want is to concatenate the dataframes, not merge them. You can do that and then sort on parameter and date, finally filling NaN with ''.

df1 = pd.DataFrame({ 'parameter' : ['A', 'B'], 'date' : ['2023-01-01', '2023-01-02'], 'column2' : ['A2_1', 'B2_2'], 'column3' : ['A3_1', 'B3_2'], 'column4' : ['A4_1', 'B4_2'], 'column5' : ['A5_1', 'B5_2'] })
df2 = pd.DataFrame({ 'parameter' : ['A', 'C'], 'date' : ['2023-01-02', '2023-01-03'], 'column4' : ['A4_2', 'C4_3'], 'column5' : ['A5_2', 'C5_3'], 'column6' : ['A6_2', 'C6_3'], 'column7' : ['A7_2', 'C7_3'] })
res = pd.concat([df1, df2]).sort_values(['parameter', 'date']).fillna('').reset_index(drop=True)
print(res)

Output:

  parameter        date column2 column3 column4 column5 column6 column7
0         A  2023-01-01    A2_1    A3_1    A4_1    A5_1
1         A  2023-01-02                    A4_2    A5_2    A6_2    A7_2
2         B  2023-01-02    B2_2    B3_2    B4_2    B5_2
3         C  2023-01-03                    C4_3    C5_3    C6_3    C7_3
Nick
  • 138,499
  • 22
  • 57
  • 95