问题:按索引合并两个数据框
嗨,我有以下数据框:
> df1
id begin conditional confidence discoveryTechnique
0 278 56 false 0.0 1
1 421 18 false 0.0 1
> df2
concept
0 A
1 B
如何合并索引以获取:
id begin conditional confidence discoveryTechnique concept
0 278 56 false 0.0 1 A
1 421 18 false 0.0 1 B
我问,因为据我了解,merge()
即df1.merge(df2)
使用列进行匹配。实际上,这样做我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4618, in merge
copy=copy, indicator=indicator)
File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 58, in merge
copy=copy, indicator=indicator)
File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 491, in __init__
self._validate_specification()
File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 812, in _validate_specification
raise MergeError('No common columns to perform merge on')
pandas.tools.merge.MergeError: No common columns to perform merge on
在索引上合并是不好的做法吗?不可能吗 如果是这样,如何将索引移到称为“索引”的新列中?
谢谢
回答 0
使用merge
,默认情况下是内部联接:
pd.merge(df1, df2, left_index=True, right_index=True)
或join
,默认情况下为左连接:
df1.join(df2)
或concat
,默认情况下为外部联接:
pd.concat([df1, df2], axis=1)
样品:
df1 = pd.DataFrame({'a':range(6),
'b':[5,3,6,9,2,4]}, index=list('abcdef'))
print (df1)
a b
a 0 5
b 1 3
c 2 6
d 3 9
e 4 2
f 5 4
df2 = pd.DataFrame({'c':range(4),
'd':[10,20,30, 40]}, index=list('abhi'))
print (df2)
c d
a 0 10
b 1 20
h 2 30
i 3 40
#default inner join
df3 = pd.merge(df1, df2, left_index=True, right_index=True)
print (df3)
a b c d
a 0 5 0 10
b 1 3 1 20
#default left join
df4 = df1.join(df2)
print (df4)
a b c d
a 0 5 0.0 10.0
b 1 3 1.0 20.0
c 2 6 NaN NaN
d 3 9 NaN NaN
e 4 2 NaN NaN
f 5 4 NaN NaN
#default outer join
df5 = pd.concat([df1, df2], axis=1)
print (df5)
a b c d
a 0.0 5.0 0.0 10.0
b 1.0 3.0 1.0 20.0
c 2.0 6.0 NaN NaN
d 3.0 9.0 NaN NaN
e 4.0 2.0 NaN NaN
f 5.0 4.0 NaN NaN
h NaN NaN 2.0 30.0
i NaN NaN 3.0 40.0
回答 1
您可以使用concat([df1,df2,…],axis = 1)来连接两个或更多个按索引对齐的DF:
pd.concat([df1, df2, df3, ...], axis=1)
或合并以通过自定义字段/索引进行串联:
# join by _common_ columns: `col1`, `col3`
pd.merge(df1, df2, on=['col1','col3'])
# join by: `df1.col1 == df2.index`
pd.merge(df1, df2, left_on='col1' right_index=True)
或参加由指数加盟:
df1.join(df2)
回答 2
默认情况下:join
是按列的左联接pd.merge
是按列的内部联接pd.concat
是按行的外部联接
pd.concat
:
采用Iterable参数。因此,它不能直接使用DataFrames(使用[df,df2]
)
DataFrame的尺寸应沿轴匹配
Join
和pd.merge
:
可以接受DataFrame参数
回答 3
一个愚蠢的错误吸引了我:由于索引dtypes
不同,联接失败。这不是很明显,因为两个表都是同一原始表的数据透视表。之后reset_index
,索引在Jupyter中看起来相同。保存到Excel时才发现…
固定于: df1[['key']] = df1[['key']].apply(pd.to_numeric)
希望这可以节省一个小时!
回答 4
如果要在熊猫中加入两个数据框,则可以简单地使用可用的属性,例如merge
或concatenate
。例如,如果我有两个数据框df1
,df2
可以通过以下方式将它们加入:
newdataframe=merge(df1,df2,left_index=True,right_index=True)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。