Home > Enterprise >  Python: merging two datasets via df.merge() is simply not working
Python: merging two datasets via df.merge() is simply not working

Time:05-22

I simply want to merg two datasets.

Sample df1:

ID Name
0 73 Dan
1 74 Emily
2 75 Kenny
... ... ...
333333 333407 Liz

Sample df2:

Intention Product ID
0 buy 1001
1 sell 1002
2 buy 1002
... ... ...
333333 buy 1011

I want the result to look like this:

ID Name Intention Product
0 73 Dan buy 1001
1 74 Emily sell 1002
2 75 Kenny buy 1002
... ... ... ... ...
333333 333407 Liz buy 1011

I always used to do it this way and it always worked:

df1.merge(df2, left_on=df1.index, right_on=df2.merge)

But now I am getting an error: ValueError: Unable to fill values because RangeIndex cannot contain NA

These datasets have the same number of rows, so the indexes. Cannot understand what's wrong. What do you think?

CodePudding user response:

You can try pd.concat

df = pd.concat([df1, df2], axis=1)

Or with

df = df1.merge(df2, left_index=True, right_index=True)
  • Related