垂直合并2个数据框 [英] merging 2 dataframes vertically
本文介绍了垂直合并2个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个数据框,每个框有2列(相同的列名).我想垂直合并它们,以得到一个新的数据框.
I have 2 dataframes that have 2 columns each (same column names). I want to merge them vertically to end up having a new dataframe.
这样做
newdf = df.merge(df1,how='left',on=['Col1','Col2'])
新df仅包含df
中的行,而没有df1
中的行.发生这种情况的任何原因是什么?
The new df has only the rows from df
and none of the rows from df1
. Any reasons why this might happen?
Col1 Col2
asd 1232
cac 2324
.....
,df1
是:
Col1 Col2
afaf 1213
asas 4353
新数据框newdf
应为:
Col1 Col2
asd 1232
cac 2324
afaf 1213
asas 4353
推荐答案
如果您不想按原样使用索引值,则可以使用append
并使用ignore_index
.
You could use append
and use ignore_index
if you don't want to use the index values as is.
In [14]: df1.append(df2)
Out[14]:
Col1 Col2
0 asd 1232
1 cac 2324
0 afaf 1213
1 asas 4353
In [15]: df1.append(df2, ignore_index=True)
Out[15]:
Col1 Col2
0 asd 1232
1 cac 2324
2 afaf 1213
3 asas 4353
或使用pd.concat
In [16]: pd.concat([df1, df2])
Out[16]:
Col1 Col2
0 asd 1232
1 cac 2324
0 afaf 1213
1 asas 4353
In [17]: pd.concat([df1, df2], ignore_index=True)
Out[17]:
Col1 Col2
0 asd 1232
1 cac 2324
2 afaf 1213
3 asas 4353
这篇关于垂直合并2个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文