在 Pandas 中组合具有不同索引的数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46755146/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:38:22 来源:igfitidea点击:
Combine Dataframes With Different Indexes in Pandas
提问by jojo
I have two dataframes:
我有两个数据框:
df1 : here index is ip
accountname name
ip
192.168.1.1 aaaa john doe
192.168.1.2 bbbb jane doe
df2 : index is accountname
gsm
accountname
aaaa 850
bbbb 860
cccc 870
I have to combine two dataframe and add gsm column to df1.
我必须组合两个数据框并将 gsm 列添加到 df1。
ip accountname name gsm
0 192.168.1.1 aaaa john doe 850
1 192.168.1.2 bbbb jane doe 860
These dataframes has different indexes and I couldnt reach right data. any advice would be appreciated.
这些数据帧具有不同的索引,我无法获得正确的数据。任何意见,将不胜感激。
回答by Zero
You could use merge
with index
as well.
你也可以使用merge
with index
。
In [2313]: df1.merge(df2, left_on='accountname', right_index=True).reset_index()
Out[2313]:
ip accountname name gsm
0 192.168.1.1 aaaa john doe 850
1 192.168.1.2 bbbb jane doe 860