我有以下数据帧:
> 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
在索引上合并是不好的做法吗?这不可能吗?如果是这样,我如何将索引转移到一个名为" index“的新列中?
发布于 2016-11-07 22:54:37
您可以使用concat([df1, df2, ...], axis=1)来连接两个或更多按索引对齐的DFs:
pd.concat([df1, df2, df3, ...], axis=1)
或用于通过自定义字段/索引连接的merge:
# 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)
或者用于通过索引连接的join:
df1.join(df2)
发布于 2019-07-26 11:06:04
默认情况下:
join
是按列的左连接
pd.merge
是按列的内连接
pd.concat
是逐行的外连接
pd.concat
takes Iterable arguments. Thus, it cannot take DataFrames directly (use `[df,df2]`)
Dimensions of DataFrame should match along axis
Join
和pd.merge
can take DataFrame arguments
发布于 2018-08-27 19:02:24
一个愚蠢的bug让我抓狂:连接失败是因为索引dtypes
不同。这并不明显,因为两个表都是同一原始表的数据透视表。在reset_index
之后,这些索引在Jupyter中看起来是一样的。只有在保存到Excel时才会出现...
我用:df1[['key']] = df1[['key']].apply(pd.to_numeric)
修复了它
希望这能为某些人节省一个小时!
https://stackoverflow.com/questions/40468069
复制相似问题