Appending two dataframes with same columns, different order(附加两个具有相同列、不同顺序的数据框)
问题描述
我有两个 pandas 数据框.
I have two pandas dataframes.
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.
这篇关于附加两个具有相同列、不同顺序的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:附加两个具有相同列、不同顺序的数据框


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01