ScientiaEtVeritas
ScientiaEtVeritas

Reputation: 5278

Pandas: Inner Join returns no 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 to inner join these DataFrames with df.merge:

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

But it doesn't return any rows, although the column name is identical.

Upvotes: 1

Views: 3411

Answers (1)

jezrael
jezrael

Reputation: 863196

There is problem different dtypes of column CommitID.

Need check them by:

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

And then convert by astype to 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)

Yur code can be simplify - omit default how='inner and 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 same joined columns in both DataFrames:

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

Upvotes: 3

Related Questions