追加两个具有相同列,不同顺序的数据框 [英] Appending two dataframes with same columns, different order

查看:116
本文介绍了追加两个具有相同列,不同顺序的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个熊猫数据框.

noclickDF = DataFrame([[0,123,321],[0,1543,432]], columns=['click', 'id','location'])
clickDF = DataFrame([[1,123,421],[1,1543,436]], columns=['click', 'location','id'])

我只是想加入,使最终的DF看起来像这样:

I simply want to join such that the final DF will look like:

click  |  id   |   location
0         123        321
0         1543       432
1         421        123
1         436       1543

如您所见,两个原始DF的列名相同,但顺序不同.列中也没有连接.

As you can see the column names of both original DF's are the same, but not in the same order. Also there is no join in a column.

推荐答案

您还可以使用 pd.concat :

In [36]: pd.concat([noclickDF, clickDF], ignore_index=True)
Out[36]: 
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543

在内部,DataFrame.append调用pd.concat. DataFrame.append具有用于处理各种类型的输入的代码,例如系列,元组,列表和字典.如果将其传递给DataFrame,它将直接传递到pd.concat,因此使用pd.concat会更直接.

Under the hood, DataFrame.append calls pd.concat. DataFrame.append has code for handling various types of input, such as Series, tuples, lists and dicts. If you pass it a DataFrame, it passes straight through to pd.concat, so using pd.concat is a bit more direct.

这篇关于追加两个具有相同列,不同顺序的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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