pandas 堆叠两个熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29351840/
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-13 23:08:02  来源:igfitidea点击:

Stack two pandas data frames

pythonpandas

提问by user308827

How do I stack the following 2 dataframes:

如何堆叠以下 2 个数据帧:

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

to give the following result:

给出以下结果:

    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)

使用Pandas合并操作不起作用,因为它只是水平排列数据框(而不是垂直排列,这正是我想要的)

回答by tnknepp

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