pandas 6 合并数据 concat, append 垂直合并,数据会变高/长
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 垂直合并,数据会变高/长的更多相关文章
- 初识Javascript.03 -- switch、自增、while循环、for、break、continue、数组、遍历数组、合并数组concat
除了注意大小写,别的木啥了 Switch语句 Switch(变量){ case 1: 如果变量和1的值相同,执行该处代码 break; case 2: 如果变量和2的值相同,执行该处代码 break; ...
- (Sql Server)数据的拆分和合并
(Sql Server)数据的拆分和合并 背景: 今天遇到了数据合并和拆分的问题,尝试了几种写法.但大致可分为两类:一.原始写法.二.Sql Server 2005之后支持的写法.第一种写法复杂而且效 ...
- concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
var arr1 = ['a', 'b', 'c']; var arr2 = ['d', 'e', 'f']; var arr3 = arr1.concat(arr2); // arr3 is a n ...
- 多条SQL语句对查询结果集的垂直合并,以及表设计时如何冗余字段
需求引入 你有一个销售单表A 和一个销售单详情表B 和一个收付款记录表C A---->B 一对多 A---->C一对多 如果一个销售单有两个详情,三条收款记录 对一个销售单 我们想查询 ...
- Excel宏录制、数据透视表、合并多个页签
前段时间做数据分析的时候,遇到很多报表文件需要处理,在此期间学习了很多Excel操作,特此做笔记回顾. Excel宏录制 打开开发者工具 打开Excel文件,选择”文件”-->“选项”--> ...
- gridview 合并单元格 并原样导出数据
使用的方式都是比较简单的,asp.net 如何进行数据的导出有好多种方法,大家可以在网上找到, 一下提供一些合并并原样输出的一个简单的代码: public void ToExcel(System.We ...
- oracle 多列数据相同,部分列数据不同合并不相同列数据
出现这样一种情况: 前面列数据一致,最后remark数据不同,将remark合并成 解决办法: 最后一列:结果详情: 使用到的语句为: select a,b,c,wm_concat(d) d,wm_c ...
- 关于表格合并span-method方法的补充(表格数据由后台动态返回)
之前写了一些关于element-ui表格合并的方法,不过用的数据都是确定的数据(死数据),但是很多时候我们的数据都是通过后台获得的,数据不稳定,这个时候使用表格合并就需要先处理一下数据,先看一下一种很 ...
- 【转载】C#的Merge方法合并两个DataTable对象的数据
在C#中的Datatable类中,可以使用DataTable类的Merge方法对两个相同结构的DataTable对象进行求并集运算,将两个DataTable对象的数据行合并到其中一个DataTable ...
随机推荐
- 工作需求——VBA操作打印机
因为最近做的事情比较多,平时也多用EXCEL,所以顺便学习EXCEL的功能性的东西 转载:https://msdn.microsoft.com/zh-tw/vba/excel-vba/articles ...
- redis 篇 - hash
hash 可以认为是 python 中的字典 field 不允许重复 string类型的field和value的映射表 每个hash可以存储 232 - 1 键值对(40多亿) 方法 hest key ...
- IOS - 总结(网络状态变更)
- (void)initNetworkMonitor { NSURL *baseURL = [NSURL URLWithString:@"http://www.baidu.com/" ...
- docker mysql pxc集群(percona-xtradb-cluster)
docker pull percona/percona-xtradb-cluster docker tag percona/percona-xtradb-cluster pxc docker netw ...
- django-10-中间件和上下文管理器
<<<中间件的引入>>> 用户<->中间件<->url->视图 在app目录里面 middleware.py (1)中间件就是一个 ...
- Git:与eclipse搭配使用
Git:与eclipse搭配使用 1)工程初始化为本地库 工程 ——>右键 ——>Team ——Share Project 在该目录下创建了本地库 这里可以设置用户签名 2)Eclipse ...
- js获取当地时间并且拼接时间格式的三种方式
js获取当地时间并且拼接时间格式,在stackoverflow上有人在问,查了资料,各种方法将时间格式改成任意自己想要的样式. 1. var date = new Date(+new Date()+8 ...
- PatentTips - Increasing turbo mode residency of a processor
BACKGROUND Many modern operating systems (OS's) use the Advanced Configuration and Power Interface ( ...
- HDU 4314 Contest 2
可以知道,逃出的人中,最后一个应当是A+B最长的,这是很容易发现的.那么,最选逃出去的必定是A+B最短的.这符合最优. 于是,可以把各小矮人按A+B的和由大到小排序.定义DP[i][j]为i个人中逃出 ...
- [Python + Unit Testing] Write Your First Python Unit Test with pytest
In this lesson you will create a new project with a virtual environment and write your first unit te ...