简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM