文章目录

  • 1.前言
  • 2.垂直合并(axis = 0)
  • 3.join(合并方式)
  • 4.join_axes
  • 5.append (添加数据)

1.前言

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

2.垂直合并(axis = 0)

axis=0是预设值,因此未设定任何参数时,函数默认axis=0

import pandas as pd
import numpy as npdf1 = pd.DataFrame(np.ones((4,5))*6, columns = ['a','b','c','d','e'])
df2 = pd.DataFrame(np.ones((4,5))*7, columns = ['a','b','c','d','e'])
df3 = pd.DataFrame(np.ones((4,5))*8, columns = ['a','b','c','d','e'])res = pd.concat([df1,df2,df3],axis = 0)
print(res)#输出a    b    c    d    e
0  6.0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0
0  7.0  7.0  7.0  7.0  7.0
1  7.0  7.0  7.0  7.0  7.0
2  7.0  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0  7.0
0  8.0  8.0  8.0  8.0  8.0
1  8.0  8.0  8.0  8.0  8.0
2  8.0  8.0  8.0  8.0  8.0
3  8.0  8.0  8.0  8.0  8.0

仔细观察会发现结果的index是0, 1, 2, 0, 1, 2, 0, 1, 2,若要将index重置,添加开关ignore_index = True:

import pandas as pd
import numpy as npdf1 = pd.DataFrame(np.ones((4,5))*6, columns = ['a','b','c','d','e'])
df2 = pd.DataFrame(np.ones((4,5))*7, columns = ['a','b','c','d','e'])
df3 = pd.DataFrame(np.ones((4,5))*8, columns = ['a','b','c','d','e'])res = pd.concat([df1,df2,df3],axis = 0,ignore_index = True)
print(res)#输出a    b    c    d    e
0   6.0  6.0  6.0  6.0  6.0
1   6.0  6.0  6.0  6.0  6.0
2   6.0  6.0  6.0  6.0  6.0
3   6.0  6.0  6.0  6.0  6.0
4   7.0  7.0  7.0  7.0  7.0
5   7.0  7.0  7.0  7.0  7.0
6   7.0  7.0  7.0  7.0  7.0
7   7.0  7.0  7.0  7.0  7.0
8   8.0  8.0  8.0  8.0  8.0
9   8.0  8.0  8.0  8.0  8.0
10  8.0  8.0  8.0  8.0  8.0
11  8.0  8.0  8.0  8.0  8.0

3.join(合并方式)

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

import pandas as pd
import numpy as npdf1 = pd.DataFrame(np.ones((4,5))*6, columns = ['a','b','c','d','e'])
df2 = pd.DataFrame(np.ones((4,5))*7, columns = ['b','c','d','e','f'])
print(df1)
print('\n')
print(df2)
res = pd.concat([df1,df2],join = 'outer')   #纵向"外"合并df1与df2
print(res)#输出a    b    c    d    e
0  6.0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0b    c    d    e    f
0  7.0  7.0  7.0  7.0  7.0
1  7.0  7.0  7.0  7.0  7.0
2  7.0  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0  7.0a    b    c    d    e    f
0  6.0  6.0  6.0  6.0  6.0  NaN
1  6.0  6.0  6.0  6.0  6.0  NaN
2  6.0  6.0  6.0  6.0  6.0  NaN
3  6.0  6.0  6.0  6.0  6.0  NaN
0  NaN  7.0  7.0  7.0  7.0  7.0
1  NaN  7.0  7.0  7.0  7.0  7.0
2  NaN  7.0  7.0  7.0  7.0  7.0
3  NaN  7.0  7.0  7.0  7.0  7.0

原理同上个例子的说明,但只有相同的column合并在一起,其他的会被抛弃。

import numpy as np
import pandas as pddf1 = pd.DataFrame(np.ones((4,5))*6, columns = ['a','b','c','d','e'])
df2 = pd.DataFrame(np.ones((4,5))*7, columns = ['b','c','d','e','f'])print(df1)
print('\n')
print(df2)
print('\n')
res = pd.concat([df1,df2],axis = 0, join = 'inner')
print(res)
print('\n')
res_sort = pd.concat([df1,df2], axis = 0, join = 'inner',ignore_index = True)  #重置index并打印结果
print(res_sort)#输出a    b    c    d    e
0  6.0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0b    c    d    e    f
0  7.0  7.0  7.0  7.0  7.0
1  7.0  7.0  7.0  7.0  7.0
2  7.0  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0  7.0b    c    d    e
0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0
0  7.0  7.0  7.0  7.0
1  7.0  7.0  7.0  7.0
2  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0b    c    d    e
0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0
4  7.0  7.0  7.0  7.0
5  7.0  7.0  7.0  7.0
6  7.0  7.0  7.0  7.0
7  7.0  7.0  7.0  7.0

4.join_axes

import numpy as np
import pandas as pddf1 = pd.DataFrame(np.ones((4,5))*6,columns = ['a','b','c','d','e'],index = [1,2,3,4])
df2 = pd.DataFrame(np.ones((4,5))*7,columns = ['b','c','d','e','f'],index = [2,3,4,5])print(df1)
print('\n')
print(df2)
print('\n')
res = pd.concat([df1,df2],axis = 1)    #原始合并,没有的行补充NaN
print(res)
print('\n')
res_sort = pd.concat([df1,df2],axis = 1,join_axes = [df1.index])   #按照df1的index合并
print(res_sort)#输出a    b    c    d    e
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0
4  6.0  6.0  6.0  6.0  6.0b    c    d    e    f
2  7.0  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0  7.0
4  7.0  7.0  7.0  7.0  7.0
5  7.0  7.0  7.0  7.0  7.0a    b    c    d    e    b    c    d    e    f
1  6.0  6.0  6.0  6.0  6.0  NaN  NaN  NaN  NaN  NaN
2  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0
3  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0
4  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0
5  NaN  NaN  NaN  NaN  NaN  7.0  7.0  7.0  7.0  7.0a    b    c    d    e    b    c    d    e    f
1  6.0  6.0  6.0  6.0  6.0  NaN  NaN  NaN  NaN  NaN
2  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0
3  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0
4  6.0  6.0  6.0  6.0  6.0  7.0  7.0  7.0  7.0  7.0

5.append (添加数据)

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

import numpy as np
import pandas as pddf1 = pd.DataFrame(np.ones((4,5))*6,columns = ['a','b','c','d','e'],index = [1,2,3,4])
df2 = pd.DataFrame(np.ones((4,5))*7,columns = ['b','c','d','e','f'],index = [2,3,4,5])
df3 = pd.DataFrame(np.ones((4,5))*8,columns = ['c','d','e','f','g'],index = [3,4,5,6])
s1 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(df1)
print(df2)res_df1_df2 = df1.append(df2, ignore_index = True)    #将df2合并到df1的下面,以及重置index,并打印出结果
print(res_df1_df2)
res_df1_df2_df3 = df1.append([df2,df3],ignore_index = True)   #合并多个df,将df2与df3合并至df1的下面,以及重置index,并打印出结果
print(res_df1_df2_df3)
res_series = df1.append(s1,ignore_index = True)
print(res_series)#输出a    b    c    d    e
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0
4  6.0  6.0  6.0  6.0  6.0b    c    d    e    f
2  7.0  7.0  7.0  7.0  7.0
3  7.0  7.0  7.0  7.0  7.0
4  7.0  7.0  7.0  7.0  7.0
5  7.0  7.0  7.0  7.0  7.0a    b    c    d    e    f
0  6.0  6.0  6.0  6.0  6.0  NaN
1  6.0  6.0  6.0  6.0  6.0  NaN
2  6.0  6.0  6.0  6.0  6.0  NaN
3  6.0  6.0  6.0  6.0  6.0  NaN
4  NaN  7.0  7.0  7.0  7.0  7.0
5  NaN  7.0  7.0  7.0  7.0  7.0
6  NaN  7.0  7.0  7.0  7.0  7.0
7  NaN  7.0  7.0  7.0  7.0  7.0a    b    c    d    e    f    g
0   6.0  6.0  6.0  6.0  6.0  NaN  NaN
1   6.0  6.0  6.0  6.0  6.0  NaN  NaN
2   6.0  6.0  6.0  6.0  6.0  NaN  NaN
3   6.0  6.0  6.0  6.0  6.0  NaN  NaN
4   NaN  7.0  7.0  7.0  7.0  7.0  NaN
5   NaN  7.0  7.0  7.0  7.0  7.0  NaN
6   NaN  7.0  7.0  7.0  7.0  7.0  NaN
7   NaN  7.0  7.0  7.0  7.0  7.0  NaN
8   NaN  NaN  8.0  8.0  8.0  8.0  8.0
9   NaN  NaN  8.0  8.0  8.0  8.0  8.0
10  NaN  NaN  8.0  8.0  8.0  8.0  8.0
11  NaN  NaN  8.0  8.0  8.0  8.0  8.0a    b    c    d    e
0  6.0  6.0  6.0  6.0  6.0
1  6.0  6.0  6.0  6.0  6.0
2  6.0  6.0  6.0  6.0  6.0
3  6.0  6.0  6.0  6.0  6.0
4  1.0  2.0  3.0  4.0  5.0

Pandas——concat(合并)相关推荐

  1. PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 47784 pandas作者Wes McKinney 在【PYTHON FOR DATA ANALYS

    PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 标签: 47784 编辑 删除 pandas作者Wes McKinney 在[PYTHON FOR DA ...

  2. 【Python】图解Pandas数据合并:concat、join、append

    公众号:尤而小屋 作者:Peter 编辑:Peter 图解pandas数据合并:concat+join+append 在上一篇文章中介绍过pandas中最为常用的一个合并函数merge的使用,本文中介 ...

  3. pandas数据合并:concat、join、append

    公众号:尤而小屋 作者:Peter 编辑:Peter 大家好,我是Peter~ 图解pandas数据合并:concat+join+append 在上一篇文章中介绍过pandas中最为常用的一个合并函数 ...

  4. pandas向下合并多个excel文件,注意concat合并出现错位混乱不对齐

    一.所有excel放在文件夹合并:向下合并 import pandas as pd import os def concat_excel(path,save_name):file_name_list ...

  5. Python 数据合并方法 —— Pandas concat() 详解

    详解concat 参数说明 concat说明 ignore_index 示例 Example Reference 参数说明 pandas.concat(objs, axis=0, join='oute ...

  6. Pandas知识点-合并操作combine

    Pandas知识点-合并操作combine combine是联合的意思,在Pandas中,combine()方法也是一种实现合并的方法,本文介绍combine()方法的用法. 一.combine_fi ...

  7. Pandas快速合并多张excel表格

    目录 一.Excel表格命名存在规律的情况下 二.Excel表格文件名不规律的情况下 1.首先将所有excel表格放到一个文件夹下面 2.用pandas进行合并 三.身份证号合并乱码解决 最近用Pan ...

  8. 【python数据分析】pandas数据合并

    pandas数据合并 使用contact,append,merge完成数据集合并 自己学习用,欢迎大佬指正. 1.concat pd.concat()可以合并series和DataFrame对象,默认 ...

  9. 阿里面试题:Pandas中合并数据的5个函数,各有千秋!

    前几天在一个群里面,看到一位朋友,说到自己的阿里面试,被问了一些关于pandas的使用.其中一个问题是:pandas中合并数据的5中方法. 今天借着这个机会,就为大家盘点一下pandas中合并数据的5 ...

  10. pandas纵向合并数据

    pandas纵向合并数据

最新文章

  1. 在CentOS 6.3 64bit上安装FTP服务器vsftpd 2.2.2
  2. linux主机密钥管理,管理ssh主机和私钥的最佳方法
  3. mysql 通过sock来登陆
  4. 如何在私有链部署智能合约
  5. boost::sort相关的测试程序
  6. Holedox Moving
  7. 微信小程序正则判断姓名和手机号
  8. 很好用的软件 RouterPassView
  9. springboot系列六、springboot配置错误页面及全局异常
  10. Dynamic Programming之Longest Increasing Subsequence (LIS)问题
  11. ValueError: This model has not yet been built. Build the model first by calling `build()` or calling
  12. Java的GUI学习十一(编程菜单)
  13. 哨兵系列卫星介绍与下载教程
  14. 单片机_第1章 单片机基础知识概述
  15. linux网络设置在哪里,虚拟机里,linux网络设置在哪啊,我没找到
  16. 9.3. Mathematical Functions and Operators
  17. 【涂鸦物联网足迹】涂鸦云平台接口列表—万能红外遥控器
  18. 微信小程序--小程序及微信生态圈
  19. MFC 修改字体的颜色
  20. 解决pip无法更新问题的简单方法:You are using pip version 20.1.2, however version 20.2.2 is available.......问题 的完

热门文章

  1. 【论文笔记】分层强化学习鼻祖:Feudal Reinforcement Learning 1993
  2. Python3 有序字典—OrderedDict()
  3. Python List sort方法无效
  4. User Agent跨站攻击
  5. android layout 渲染,java – 渲染android.support.design.widget.CoordinatorLayout的问题
  6. 程序员最深情的告白——《致对象》
  7. idea中刷新maven依赖,总是拉不下来
  8. idea中push到github或gitee过程中的常见错误记录
  9. android 讲程序设为默认主屏幕_轻松搞定 PC 副屏,双屏幕更方便!
  10. 软件单元测试(Unit Test )最佳实践