堆叠两个 pandas 数据框 [英] Stack two pandas data frames
本文介绍了堆叠两个 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何堆叠以下 2 个数据帧:
How do I stack the following 2 dataframes:
df1
hzdept_r hzdepb_r sandtotal_r
0 0 114 0
1 114 152 92.1
df2
hzdept_r hzdepb_r sandtotal_r
0 0 23 83.5
1 23 152 45
给出以下结果:
hzdept_r hzdepb_r sandtotal_r
0 0 114 0
1 114 152 92.1
2 0 23 83.5
3 23 152 45
使用熊猫合并操作不起作用,因为它只是水平排列数据框(而不是垂直排列,这正是我想要的)
Using the pandas merge operations does not work since it just lines the dataframes horizontally (and not vertically, which is what I want)
推荐答案
In [5]: a = pd.DataFrame(data=np.random.randint(0,100,(2,5)),columns=list('ABCDE'))
In [6]: b = pd.DataFrame(data=np.random.randint(0,100,(2,5)),columns=list('ABCDE'))
In [7]: c = pd.concat([a,b],ignore_index=True)
In [8]: c
Out[8]:
A B C D E
0 12 56 62 35 20
1 10 71 63 0 70
2 61 72 29 10 71
3 88 82 39 73 94
这篇关于堆叠两个 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文