0

Thank you for helping, I really have trouble with the merge of dataframes (I wanna merge two frames with the following conditions) ,however it fails "Unintended result in merged dataframes yellow-highlighted in picture 3).

I wanna merge based on the first two columns. For row, 1). If column one and two in the two frames matched together. --> then combine into a single row. 2). If either column one or two do not match in the two frames , but the other one matched --> create a new row. 3). If both column one and two do not match in the two frames --> create a new row.

Dataframe (1)

enter image description here

Dataframe (2) enter image description here

enter image description here

  • Main problem is " in the last picture, the merged dataframes showing incorrect result for date 2022-05-01 and 2022-04-30 with two rows showing different numbers even though the first two conditions are exactly the same. How to fix" Thank you! – ip stanley May 01 '22 at 04:13
  • 2
    [Please do not upload images of code/errors when asking a question](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). And please provide a MRE (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)). – Timus May 01 '22 at 12:57

1 Answers1

0

You can try specifying a list of columns to merge on.

Output_list_sort[0].merge(Output_list_sort[1], on=['省市', '市', '類型'], how='inner')
greco
  • 304
  • 4
  • 11