【发布时间】:2016-10-24 10:33:31
【问题描述】:
我有 2 个 DataFrames 我想合并。我查看了文档并尝试执行以下操作,但对如何执行感到困惑。就像我说的,我有 2 个DataFrames:
df1:
id name type currency
0 BTA.S Applewood Hard GBp
1 VOD.S Softwood Soft GBp
和
df2:
id
BTA.S 301.221525
VOD.S 213.791400
我想回来:
id name type currency price
0 BTA.S Applewood Hard GBp 301.221525
1 VOD.S Softwood Soft GBp 213.791400
df2 中的价格列与 df1 合并的位置。 (只是为了让你知道在我完成时会有更多的木材类型)。
我尝试了几种方法:
Result = df1.merge(df2[['*.S']], left_on='id', right_index=True)
我遇到异常的地方:
ValueError: can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>
和
Result = pd.concat([Df1, Df2], axis=1, ignore_index=True)
我得到异常的地方:
ValueError: labels ['type'] not contained in axis
但我有点糊涂了。
【问题讨论】: