Python 合并两个数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37968785/
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
Merging two DataFrames
提问by Stacey
I have 2 DataFrames
which I would like to merge. I have looked at the documentation and tried to perform the following operation but an getting confused as to how to do it. Like I said I have 2 DataFrames
:
我有 2 个DataFrames
我想合并。我查看了文档并尝试执行以下操作,但对如何执行感到困惑。就像我说的,我有 2 个DataFrames
:
df1:
id name type currency
0 BTA.S Applewood Hard GBp
1 VOD.S Softwood Soft GBp
and
和
df2:
id
BTA.S 301.221525
VOD.S 213.791400
and I would like to return:
我想返回:
id name type currency price
0 BTA.S Applewood Hard GBp 301.221525
1 VOD.S Softwood Soft GBp 213.791400
Where the price column from the df2 is merged with df1. (Just to let you know there will be alot more wood types by the time I've finished).
df2 中的 price 列与 df1 合并的地方。(只是为了让您知道到我完成时会有更多的木材类型)。
I have tried a few methods of doing this:
我尝试了几种方法来做到这一点:
Result = df1.merge(df2[['*.S']], left_on='id', right_index=True)
where I met the exception:
我遇到异常的地方:
ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
and
和
Result = pd.concat([Df1, Df2], axis=1, ignore_index=True)
where I get the exception:
我得到异常的地方:
ValueError: labels ['type'] not contained in axis
But I am getting confused.
但我越来越困惑。
回答by Stefan
The error message indicates that df2
is of type pd.Series
. You need to convert df2
.to_frame()
as .merge()
needs a pd.DataFrame()
input (see docs):
错误消息表明它df2
的类型为pd.Series
。您需要df2
.to_frame()
根据.merge()
需要转换pd.DataFrame()
输入(请参阅文档):
df1.merge(df2[['*.S']].to_frame(), left_on='id', right_index=True)
while you probably also just could:
而你可能也可以:
df1.merge(df2.to_frame(), left_on='id', right_index=True)
Alternatively, you can use pd.DataFrame.join()
which accepts a pd.Series
.
或者,您可以使用pd.DataFrame.join()
which 接受pd.Series
.
回答by michael_j_ward
This error means that one of your objects is nota pandas Data Frame.
此错误意味着您的对象之一不是Pandas 数据框。
ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
To prove this to yourself,
为了向自己证明这一点,
print(type(df2))
And that should output pandas.core.series.Series
这应该输出 pandas.core.series.Series
To achieve your desired result,
为了达到你想要的结果,
df2 = df2.to_frame().reset_index()
df2.columns = ['id', 'price']
df1.merge(df2)
Outputs:
输出:
id name type currency price
0 BTA.S Applewood Hard GBp 301.221525
1 VOD.S Softwood Soft GBp 213.791400
回答by Alex Monras
You can simply add df2 (which is a Series, not a DataFrame) as a new column
您可以简单地将 df2(它是一个系列,而不是一个 DataFrame)添加为一个新列
df['price']=df2
回答by wa007
use to_frame() or updata your pandas;
使用 to_frame() 或更新你的熊猫;
join Series with Dataframe is accepted in new pandas version
join Series with Dataframe 在新的熊猫版本中被接受