pandas处理多组数据的时候往往会要用到数据的合并处理,使用 concat是一种基本的合并方式.而且concat中有很多参数可以调整,合并成你想要的数据形式.

1、axis(合并方向):axis=0是预设值,因此未设定任何参数时,函数默认axis=0

>>> import pandas as pd
>>> import numpy as np
#定义资料集
>>> df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'])
>>> df2 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d'])
>>> df3 = pd.DataFrame(np.ones((3,4))*2, columns=['a','b','c','d'])
#concat纵向合并
>>> res = pd.concat([df1, df2, df3], axis=0)
>>> print(res)
a b c d
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0
0 2.0 2.0 2.0 2.0
1 2.0 2.0 2.0 2.0
2 2.0 2.0 2.0 2.0

仔细观察会发现结果的index是0, 1, 2, 0, 1, 2, 0, 1, 2,若要将index重置,请看下面。

2、ignore——index(重置index)

#承上一个例子,并将index_ignore设定为True
>>> res = pd.concat([df1, df2, df3], axis=0, ignore_index=True)
>>> print(res)
a b c d
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 1.0 1.0 1.0 1.0
4 1.0 1.0 1.0 1.0
5 1.0 1.0 1.0 1.0
6 2.0 2.0 2.0 2.0
7 2.0 2.0 2.0 2.0
8 2.0 2.0 2.0 2.0

结果的index变0, 1, 2, 3, 4, 5, 6, 7, 8。

3、join(合并方式)

join='outer'为预设值,因此未设定任何参数时,函数默认join='outer'。此方式是依照column来做纵向合并,有相同的column上下合并在一起,其他独自的column个自成列,原本没有值的位置皆以NaN填充。

>>> import pandas as pd
>>> import numpy as np
>>> df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3])
>>> df2 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d','e'], index=[2,3,4])
#纵向"外"合并df1与df2
>>> res = pd.concat([df1, df2], axis=0, join='outer')
>>> print(res)
a b c d e
1 0.0 0.0 0.0 0.0 NaN
2 0.0 0.0 0.0 0.0 NaN
3 0.0 0.0 0.0 0.0 NaN
2 NaN 1.0 1.0 1.0 1.0
3 NaN 1.0 1.0 1.0 1.0
4 NaN 1.0 1.0 1.0 1.0 #原理同上个例子的说明,但只有相同的column合并在一起,其他的会被抛弃。
#纵向"内"合并df1与df2
>>> res = pd.concat([df1, df2], axis=0, join='inner')
>>> print(res)
b c d
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
2 1.0 1.0 1.0
3 1.0 1.0 1.0
4 1.0 1.0 1.0 #重置index并打印结果
>>> res = pd.concat([df1, df2], axis=0, join='inner', ignore_index=True)
>>> print(res)
b c d
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 1.0 1.0 1.0
4 1.0 1.0 1.0
5 1.0 1.0 1.0

4、join_axes(依照axes合并)

>>> import pandas as pd
>>> import numpy as np
>>> df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3])
>>> df2 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d','e'], index=[2,3,4])
#依照`df1.index`进行横向合并
>>> res = pd.concat([df1, df2], axis=1, join_axes=[df1.index])
>>> print(res)
a b c d b c d e
1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN
2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 #移除join_axes,并打印结果
>>> res = pd.concat([df1, df2], axis=1)
>>> print(res)
a b c d b c d e
1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN
2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
4 NaN NaN NaN NaN 1.0 1.0 1.0 1.0

5、append(添加数据)

append只有纵向合并,没有横向合并。

>>> import pandas as pd
>>> import numpy as np >>> df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'])
>>> df2 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d'])
>>> df3 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d'])
>>> s1 = pd.Series([1,2,3,4], index=['a','b','c','d']) #将df2合并到df1的下面,以及重置index,并打印出结果
>>> res = df1.append(df2, ignore_index=True)
>>> print(res)
a b c d
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 1.0 1.0 1.0 1.0
4 1.0 1.0 1.0 1.0
5 1.0 1.0 1.0 1.0 #合并多个df,将df2与df3合并至df1的下面,以及重置index,并打印出结果
>>> res = df1.append([df2, df3], ignore_index=True)
>>> print(res)
a b c d
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 1.0 1.0 1.0 1.0
4 1.0 1.0 1.0 1.0
5 1.0 1.0 1.0 1.0
6 1.0 1.0 1.0 1.0
7 1.0 1.0 1.0 1.0
8 1.0 1.0 1.0 1.0 #合并series,将s1合并至df1,以及重置index,并打印出结果
>>> res = df1.append(s1, ignore_index=True)
>>> print(res)
a b c d
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 1.0 2.0 3.0 4.0

Pandas 合并 concat的更多相关文章

  1. 【转】Pandas学习笔记(五)合并 concat

    Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...

  2. pandas的concat函数和append方法

    pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,keys=None, levels=None, nam ...

  3. python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)

    # python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...

  4. python pandas 合并数据函数merge join concat combine_first 区分

    pandas对象中的数据可以通过一些内置的方法进行合并:pandas.merge,pandas.concat,实例方法join,combine_first,它们的使用对象和效果都是不同的,下面进行区分 ...

  5. Pandas合并数据集之concat、combine_first方法

    轴向连接(concat) Numpy import numpy as np import pandas as pd from pandas import Series arr = np.arange( ...

  6. pandas合并数据集-【老鱼学pandas】

    有两个数据集,我们想把他们的结果根据相同的列名或索引号之类的进行合并,有点类似SQL中的从两个表中选择出不同的记录并进行合并返回. 合并 首先准备数据: import pandas as pd imp ...

  7. Pandas合并数据集之merge、join方法

    合并数据集 pandas.merge 可根据一个或多个键将不同DataFrame中的行连接起来. pandas.concat 可以沿着一条轴将多个对象堆叠到一起. combine_first merg ...

  8. Pandas 合并merge

    pandas中的merge和concat类似,但主要是用于两组有key column的数据,统一索引的数据. 通常也被用在Database的处理当中. 1.依据一组key合并 >>> ...

  9. pandas 合并数据

    1.  pandas 的merge,join 就不说了. 2.  神奇的:  concat      append 参考: PANDAS 数据合并与重塑(concat篇) 3.

随机推荐

  1. bootstrapValidator关于js,jquery动态赋值不触发验证(不能捕获“程序赋值事件”)解决办法

    //触发oninput事件 //propertychange 兼容ie678 $('#captainName').on('input propertychange', function() { }); ...

  2. Python 输出

    普通的输出 生活中的“输出”: 软件中的“输出”: python中变量的输出: print('hello world') 格式化输出 占位符% print('Hello,%s' % 'Python') ...

  3. 【C++】读取参数的类

    在C++程序中,如果我们把程序中的参数都保存在txt文本中,运行时再去读取.这样的好处是,当我们需要调参的时候,不需要每次都重新编译程序,大大提升了效率. 今日分享一份实现以上功能的代码,代码来源:h ...

  4. python if not

    判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法. 使用 ...

  5. LeetCode【88. 合并两个有序数组】

    首先想到的方法就是,假设一个nums3数组,然后,比较nums1与nums2的数值大小,然后,放在nums3中,再将nums3转移到nums1中. 实现起来很麻烦,1.没有考虑到下标问题,结果就Arr ...

  6. Altmetric

    网站名称: Altmetric 所属国家: 美国 网站分类: 科研软件 网站地址: https://www.altmetric.com Altmetric是一个新兴的指标,字面意思是替代指标,但“社会 ...

  7. REST framwork之分页器,路由器,响应器

    一 REST framwork分页器: from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination ...

  8. Mac 日常使用tips

    20180725: windows标准的键盘连接了mac如何映射键盘?最大的好处是可以向后删除,还可以一键PageUP, PageDown ref: https://support.apple.com ...

  9. python:win下将py文件打包成exe

    [环境]windows,正常运行的python文件 1.安装pyinstaller ,cmd下执行以下命令,需看到安装成功界面 pip install pyinstaller 2.cmd中进入要打包的 ...

  10. 序列化与反序列化之Kryo

    序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. 需要序列化的情况: 当你想把的内存中的对象状态保存到一个文件中或者数据库中时候: 当你想 ...