Code Doge
Code Doge

Reputation: 41

Combine 2 dataframes share the same column in pandas

I want to combine two dataframes into one

df1

 key  columnA
  1    0
  1    1
  1    2

df2

 key columnB
  1    3
  1    5
  1    7

result

 key columnA columnB
  1    0       3
  1    1       5
  1    2       7

resulting dataframe's column ordering is not important

Edit: I have tried

 pd.merge(df1, df2, on ='key', how = 'inner')

it gives me a df of

    A  key  B
    0    1  3
    0    1  5
    0    1  7
    1    1  3
    1    1  5
    1    1  7
    2    1  3
    2    1  5
    2    1  7

Upvotes: 2

Views: 5730

Answers (1)

andrew_reece
andrew_reece

Reputation: 21274

Because key has the same value on every row, you'll end up with a Cartesian join because it's unclear which 1 to match on for each key = 1. Instead, match with both key and index.

With:

df1 = pd.DataFrame({"key":np.ones(3, dtype=int), "columnA":np.arange(3)})
df2 = pd.DataFrame({"key":np.ones(3, dtype=int), "columnB":np.arange(3,9,2)})

Merge like this:

df1.merge(df2, on="key", left_index=True, right_index=True)
   columnA  key  columnB
0        0    1        3
1        1    1        5
2        2    1        7

Upvotes: 3

Related Questions