Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas Outer merge
#1
I have two data frames
dataframe 1
col1 col2
1 A
1 B
2 C
5 Z
4 Q

dataframe 2

col1 col2
1 A
1 B
2 E
3 X
4 R
4 S
I am performing an outer merge

merge = pd.merge(dataframe1, datagrame2, on="col1", how='outer')
I get

col1 col2_x col2_y
1 A A
1 A B
1 B A
1 B B
2 C E
5 Z NAN
3 NAN X
4 Q R
4 Q S

How do I get the output as below

col1 col2_x col2_y
1 A A
1 B B
2 C E
5 Z NAN
3 NAN X
4 Q R
4 Q S
Any help will be appreciated. Thank you
Reply
#2
Can't see how to get what you want, but somewhere near it.

Given

Output:
df1 col1 col2 0 1 A 1 1 B 2 2 C 3 5 Z 4 4 Q
and

Output:
df2 col1 col2 0 1 A 1 1 B 2 2 E 3 3 X 4 4 R 5 4 S
df3 = df1.merge(df2, how="outer", left_index=True, right_index=True, suffixes=('_left', '_right'))
df4 = df3[['col2_left', 'col2_right']]

Output:
df4 col2_left col2_right 0 A A 1 B B 2 C E 3 Z X 4 Q R 5 NaN S
Reply
#3
Your desired output appears arbitrary. I cannot think of any logic which would produce your output, let alone find a pandas function to implement that logic. Why is (1, A, A) good but (1, A, B) bad? If (1, A, B) is bad, why is (1, Q, R) good? Please describe the logic behind your desired output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,142 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Unindent does not match any outer intendation level -error MaartenRo 3 2,340 Jun-28-2020, 11:46 AM
Last Post: MaartenRo
  Merge CSV Column using Pandas Data Frames davidlang 1 2,678 May-01-2020, 02:43 PM
Last Post: klllmmm
  HELP! unindent does not match any outer indentation level blackjesus24 2 2,018 Jan-29-2020, 08:00 AM
Last Post: blackjesus24
  unindent does not match any outer indentation level , How can i fix it the_fire_pharaoh 2 2,564 Jan-01-2019, 10:52 AM
Last Post: the_fire_pharaoh
  Outer loop not running ted_gress 2 3,382 Aug-25-2018, 07:56 AM
Last Post: volcano63
  unindent does not match any outer indentation level tonyk334 4 5,104 Mar-18-2018, 04:40 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020