pandas join()可以工作,但是concat()失败 [英] Pandas join() works, but concat() fails
本文介绍了 pandas join()可以工作,但是concat()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Pandas 0.14.1对具有相同行索引的两个数据框进行外部联接.
I want to perform outer join of two dataframes with the same row index using Pandas 0.14.1.
df1
的形状为456,1
,df2
的形状为139,5
.
The shape of df1
is 456,1
and df2
is 139,5
.
df2
中的大多数键都在df1
中找到:
Most of the keys in df2
are found in df1
:
[in] print len(list(set(df2.index)-set(df1.index)))
[out] 16
join
起作用:
[in] df3=df1.join(df2,how='outer')
[in] df3.shape
[out] 473,6
concat
失败:
[in] df3=pd.concat([df1,df2],axis=1,join='outer')
[out] ValueError: Shape of passed values is (6, 473), indices imply (6, 472)
这可能是什么原因造成的?
What may cause this?
推荐答案
如果其中一个索引具有重复值,则可能会出现此错误.例如,
You could get this error if one of the indexes has duplicate values. For instance,
import pandas as pd
df1 = pd.DataFrame(np.random.random((5,1)), index=list('AACDE'),
columns=['foo'])
df2 = pd.DataFrame(np.random.random((4,1)), index=list('CDEF'),
columns=['bar'])
然后
In [50]: df1.join(df2, how='outer')
Out[50]:
foo bar
A 0.846814 NaN
A 0.638571 NaN
C 0.516051 0.573165
D 0.789398 0.095466
E 0.921592 0.970619
F NaN 0.061434
但是
In [51]: pd.concat([df1,df2], axis=1, join='outer')
ValueError: Shape of passed values is (2, 6), indices imply (2, 5)
这篇关于 pandas join()可以工作,但是concat()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文