垂直合并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
应该是:
The new dataframe newdf
should be:
Col1 Col2
asd 1232
cac 2324
afaf 1213
asas 4353
推荐答案
如果您不想将索引值用作是.
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屋!
查看全文