Pandas 数据框垂直合并 [英] Pandas dataframe vertical merge

查看:79
本文介绍了Pandas 数据框垂直合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于合并两个数据框的查询例如,我有 2 个数据框,如下所示:

I have a query regarding merging two dataframes For example i have 2 dataframes as below :

print(df1)

   Year   Location  
0  2013    america     
1  2008     usa       
2  2011     asia   



print(df2)

   Year Location  
0  2008      usa       
1  2008      usa       
2  2009     asia  

我的预期输出:

Year   Location  
2013    america     
2008     usa       
2011     asia

Year   Location
2008      usa       
2008      usa       
2009     asia

我现在得到的输出:

Year   Location    Year   Location
2013   america    2008    usa
2008    usa       2008    usa
2011    asia      2009    asia

我尝试过使用 pd.concatpd.merge 但没有成功

I have tried using pd.concat and pd.merge with no luck

请帮我解决以上问题

推荐答案

pd.concat([df1, df2]) 应该可以工作.如果所有列标题都相同,它会将第二个数据框的行绑定到第一个下方.此图片来自熊猫备忘单 (

pd.concat([df1, df2]) should work. If all the column headings are the same, it will bind the second dataframe's rows below the first. This graphic from a pandas cheat sheet (https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf) explains it pretty well:

这篇关于Pandas 数据框垂直合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆