from __future__ import print_function
import pandas as pd
import numpy as np

concatenating

# ignore index
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']) print(df1)
print(df2)
print(df3) > 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 > a b c d
> 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 > a b c d
> 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
res = pd.concat([df1, df2, df3], axis=0, ignore_index=True)  # 忽略掉原来的编号012012012重新排序成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
> 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
join, ('inner', 'outer')
# join, ('inner', 'outer')
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])
print(df1)
print(df2) > a b c d
> 1 0.0 0.0 0.0 0.0
> 2 0.0 0.0 0.0 0.0
> 3 0.0 0.0 0.0 0.0 > b c d e
> 2 1.0 1.0 1.0 1.0
> 3 1.0 1.0 1.0 1.0
> 4 1.0 1.0 1.0 1.0
res = pd.concat([df1, df2], axis=1, join='outer')  # 默认是outer,没有的属性值用NaN填充,求并集
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
res = pd.concat([df1, df2], axis=1, join='inner')  # 只寻找有相同属性的值,其他舍弃,求交集
print(res) > a b c d b c d e
> 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
# join_axes
res = pd.concat([df1, df2], axis=1, join_axes=[df1.index]) # 保留df1,
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

append

# append
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=['b','c','d', 'e'], index=[2,3,4])
print(df1)
print(df2) > 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 > a b c d
> 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
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
res = df1.append([df2, df3])
print(res) > a b c d e
> 0 0.0 0.0 0.0 0.0 NaN
> 1 0.0 0.0 0.0 0.0 NaN
> 2 0.0 0.0 0.0 0.0 NaN
> 0 1.0 1.0 1.0 1.0 NaN
> 1 1.0 1.0 1.0 1.0 NaN
> 2 1.0 1.0 1.0 1.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
s1 = pd.Series([1,2,3,4], index=['a','b','c','d'])
print(s1) > a 1
> b 2
> c 3
> d 4
> dtype: int64
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

END

pandas 6 合并数据 concat, append 垂直合并,数据会变高/长的更多相关文章

  1. 初识Javascript.03 -- switch、自增、while循环、for、break、continue、数组、遍历数组、合并数组concat

    除了注意大小写,别的木啥了 Switch语句 Switch(变量){ case 1: 如果变量和1的值相同,执行该处代码 break; case 2: 如果变量和2的值相同,执行该处代码 break; ...

  2. (Sql Server)数据的拆分和合并

    (Sql Server)数据的拆分和合并 背景: 今天遇到了数据合并和拆分的问题,尝试了几种写法.但大致可分为两类:一.原始写法.二.Sql Server 2005之后支持的写法.第一种写法复杂而且效 ...

  3. concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

    var arr1 = ['a', 'b', 'c']; var arr2 = ['d', 'e', 'f']; var arr3 = arr1.concat(arr2); // arr3 is a n ...

  4. 多条SQL语句对查询结果集的垂直合并,以及表设计时如何冗余字段

    需求引入 你有一个销售单表A 和一个销售单详情表B 和一个收付款记录表C A---->B 一对多   A---->C一对多 如果一个销售单有两个详情,三条收款记录 对一个销售单 我们想查询 ...

  5. Excel宏录制、数据透视表、合并多个页签

    前段时间做数据分析的时候,遇到很多报表文件需要处理,在此期间学习了很多Excel操作,特此做笔记回顾. Excel宏录制 打开开发者工具 打开Excel文件,选择”文件”-->“选项”--> ...

  6. gridview 合并单元格 并原样导出数据

    使用的方式都是比较简单的,asp.net 如何进行数据的导出有好多种方法,大家可以在网上找到, 一下提供一些合并并原样输出的一个简单的代码: public void ToExcel(System.We ...

  7. oracle 多列数据相同,部分列数据不同合并不相同列数据

    出现这样一种情况: 前面列数据一致,最后remark数据不同,将remark合并成 解决办法: 最后一列:结果详情: 使用到的语句为: select a,b,c,wm_concat(d) d,wm_c ...

  8. 关于表格合并span-method方法的补充(表格数据由后台动态返回)

    之前写了一些关于element-ui表格合并的方法,不过用的数据都是确定的数据(死数据),但是很多时候我们的数据都是通过后台获得的,数据不稳定,这个时候使用表格合并就需要先处理一下数据,先看一下一种很 ...

  9. 【转载】C#的Merge方法合并两个DataTable对象的数据

    在C#中的Datatable类中,可以使用DataTable类的Merge方法对两个相同结构的DataTable对象进行求并集运算,将两个DataTable对象的数据行合并到其中一个DataTable ...

随机推荐

  1. 电子邮件的三个协议: SMTP、IMAP、POP3

    个人总结: 读完这篇文章需要10分钟 讲解了跟电子邮件有关的三个协议: SMTP(simple message transfer protocol 简单信息传输协议 IMAP (internet me ...

  2. CMDB设计

    CMDB(资产管理数据库) CMDB是所有运维工具的数据基础 CMDB包含的内容 用户管理,记录测试,开发,运维人员的用户表 业务线管理,需要记录业务的详情 项目管理,指定此项目用属于哪条业务线,以及 ...

  3. myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace

    问题描述: 1 第一次从外部导入工程或者新建工程,成功: 2 删除该工程,但是没有选择delete project contents on disk 3 再次需要该工程,导入该工程时出现警告:Some ...

  4. ZOJ 3688

    做出这题,小有成就感 本来已打算要用那个禁位的排列公式,可是,问题在于,每个阶乘前的系数r的求法是一个难点. 随便翻了翻那本美国教材<组合数学>,在容斥原理一章的习题里竟有一道类似,虽然并 ...

  5. Linux 0.11中write实现

    看了一下Linux 0.11版本号write的实现,首先它在标准头文件unistd.h中有定义 int write(int fildes, const char * buf, off_t count) ...

  6. Android自己定义矩形及selector、shape的使用

    [声明]转载请注明出处.此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail --每天写一篇博客.每天做一点技术积累! Android自己定义矩形及selector ...

  7. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  8. Mysql实战45讲 05讲深入浅出索引(下)极客时间 读书笔记

    极客时间 Mysql实战45讲 04讲深入浅出索引(下)极客时间 笔记体会: 回表:回到主键索引树搜索的过程,称为回表覆盖索引:某索引已经覆盖了查询需求,称为覆盖索引,例如:select ID fro ...

  9. @Query Annotation in Spring Data JPA--转

    原文地址:http://javabeat.net/spring-data-jpa-query/ In my previous post on Spring Data, I have explained ...

  10. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...