pandas :内部联接不返回任何行 [英] Pandas: Inner Join returns no rows
本文介绍了 pandas :内部联接不返回任何行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataFrame 1(commits
)
DataFrame 1 (commits
)
CommitID | COMMITTER
------------------------
1 | A
2 | B
3 | B
DataFrame 2(files
)
DataFrame 2 (files
)
CommitID | MOD
------------------------
1 | 0
2 | 1
3 | 7
我试图用df.merge
内部连接这些DataFrame:
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.
推荐答案
CommitID
列存在与dtypes
不同的问题.
There is problem different dtypes
of column CommitID
.
需要通过以下方式检查它们:
Need check them by:
print (files['CommitID'].dtypes)
print (commits['CommitID'].dtypes)
然后通过astype
转换为相同的内容:
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)
您的代码可以简化-省略默认how='inner
并仅使用on
:
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
或者如果两个DataFrames
中只有相同的联接列:
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
这篇关于 pandas :内部联接不返回任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文