pandas 熊猫:简单的“加入”不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10114399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:42:11  来源:igfitidea点击:

Pandas: simple 'join' not working?

pandas

提问by Phil

I like to think I'm not an idiot, but maybe I'm wrong. Can anyone explain to me why this isn't working? I can achieve the desired results using 'merge'. But I eventually need to join multiple pandasDataFramesso I need to get this method working.

我喜欢认为我不是白痴,但也许我错了。谁能向我解释为什么这不起作用?我可以使用“合并”达到预期的结果。但我最终需要加入多个,pandasDataFrames所以我需要让这个方法起作用。

In [2]: left = pandas.DataFrame({'ST_NAME': ['Oregon', 'Nebraska'], 'value': [4.685, 2.491]})

In [3]: right = pandas.DataFrame({'ST_NAME': ['Oregon', 'Nebraska'], 'value2': [6.218, 0.001]})

In [4]: left.join(right, on='ST_NAME', lsuffix='_left', rsuffix='_right')
Out[4]: 
  ST_NAME_left  value ST_NAME_right  value2
0       Oregon  4.685           NaN     NaN
1     Nebraska  2.491           NaN     NaN

回答by Wes McKinney

Try using merge:

尝试使用merge

In [14]: right
Out[14]: 
    ST_NAME  value2
0    Oregon   6.218
1  Nebraska   0.001

In [15]: merge(left, right)
Out[15]: 
    ST_NAME  value  value2
0  Nebraska  2.491   0.001
1    Oregon  4.685   6.218

In [18]: merge(left, right, on='ST_NAME', sort=False)
Out[18]: 
    ST_NAME  value  value2
0    Oregon  4.685   6.218
1  Nebraska  2.491   0.001

DataFrame.joinis a bit of legacy method and apparently doesn't do column-on-column joins (originally it did index on column using the on parameter, hence the "legacy" designation).

DataFrame.join是一种遗留方法,显然不进行列对列连接(最初它使用 on 参数对列进行索引,因此称为“遗留”名称)。

回答by Donatas Svilpa

I can confirm, Pandas join method is faulty. In my case both keys were long strings (18 characters) and result was as if pandas was only matching first couple of characters. Merge function is working properly. Please do not use join function, it should be really removed from available methods, otherwise it can mess it up big time.

我可以确认,Pandas join 方法有问题。在我的例子中,两个键都是长字符串(18 个字符),结果就像熊猫只匹配前几个字符。合并功能正常工作。请不要使用连接函数,它应该从可用的方法中真正删除,否则它会很麻烦。