合并键在python中没有公共值
我试图合并两个数据文件product and review on common key product_id,这表明没有公共值-因此我得到一个包含NaN的合并表 经过一些检查,我发现确实有共同的价值观 代码: 出于某种奇怪的原因,文件product上的名称product\u id是'\xef\xbb\xbfproduct\u id'合并键在python中没有公共值,python,merge,key,Python,Merge,Key,我试图合并两个数据文件product and review on common key product_id,这表明没有公共值-因此我得到一个包含NaN的合并表 经过一些检查,我发现确实有共同的价值观 代码: 出于某种奇怪的原因,文件product上的名称product\u id是'\xef\xbb\xbfproduct\u id' product2=product[['\xef\xbb\xbfproduct_id','page_id']].sort('\xef\xbb\xbfproduct_i
product2=product[['\xef\xbb\xbfproduct_id','page_id']].sort('\xef\xbb\xbfproduct_id')
review2=review[['product_id','review_id']]
tables=[product2,review2]
for item in tables:
print item.head(5)
print item.shape
product_id page_id
4028 1071569 39995
4435 1071570 42719
2519 1071571 27421
3992 1071572 39839
4018 1071574 39978
(4999, 2)
product_id review_id
0 1071569 1755406
1 1071569 1755407
2 1071569 1755408
3 1071569 1755409
4 1071569 1755410
(4999, 2)
product2["\xef\xbb\xbfproduct_id"][4028]==review2['product_id'][0]
True
这项简单的检查显示,至少“product id”1071569存在于两个表中。但在我尝试时:
product2["\xef\xbb\xbfproduct_id"][4028] in review2['product_id']
它返回False
有人能帮我吗?你就是这样做的:
product2["\xef\xbb\xbfproduct_id"][4028] in review2['product_id'].values
如果有帮助,您可以将review2['product_id']可视化为一个字典,其键为行号0、1、2。。。该值是该行号上对应的产品id。因此,review2['product\u id']中的product2[\xef\xbb\xbfproduct\u id][4028]不起作用,因为它搜索的行号是1071569,幸运的是您没有行号。如果数据集较大,则返回True,但这将是一个逻辑错误,因为您没有检查行号,而是检查product2数据框的product_id是否可用作reviews2数据框的product_id,而不管它在reviews2上的哪个行号。不确定您的主要问题,但是“\xef\xbb\xbf”是因为该文件是UTF-8文件,您可能会将其作为ASCII文件打开。