Pandas: Inner Join does not return rows

DataFrame 1 ( commits)

CommitID  | COMMITTER  
------------------------
  1       | A         
  2       | B         
  3       | B         

DataFrame 2 ( files)

CommitID  | MOD
------------------------
  1       | 0         
  2       | 1         
  3       | 7       

I tried combining these DataFrames with df.merge:

files.merge(right=commits, how='inner',left_on="CommitID", right_on="CommitID")

But it does not return any rows, although the column name is identical.

+1
source share
1 answer

The problem is different from the dtypescolumn CommitID.

Need to check them out:

print (files['CommitID'].dtypes)
print (commits['CommitID'].dtypes)

And then convert astypeto the same:

#change only object
files['CommitID'] = files['CommitID'].astype(int)
commits['CommitID'] = commits['CommitID'].astype(int)

#change only int
files['CommitID'] = files['CommitID'].astype(str)
commits['CommitID'] = commits['CommitID'].astype(str)

Jurassic code can be simplified - skip default how='innerand use only on:

df = files.merge(right=commits, on="CommitID")
print (df)
   CommitID  MOD COMMITTER
0         1    0         A
1         2    1         B
2         3    7         B

Or, if only the same related columns in DataFrames:

df = files.merge(right=commits)
print (df)
   CommitID  MOD COMMITTER
0         1    0         A
1         2    1         B
2         3    7         B
+1
source

Source: https://habr.com/ru/post/1680827/


All Articles