轴向连接(concat)

Numpy

import numpy as np
import pandas as pd
from pandas import Series arr = np.arange(12).reshape(3,4)
arr array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]) # axis默认为行,想合并列可以设置axis=1
np.concatenate([arr,arr]) array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

pandas对象的轴向连接

# 三个没有重叠的索引合在一起
s1 = Series([0,1],index=['a','b'])
s2 = Series([2,3,4],index=['c','d','e'])
s3 = Series([5,6], index=['f','g'])
# concat对象里面需要接受一个可迭代的对象
pd.concat([s1,s2,s3]) a 0
b 1
c 2
d 3
e 4
f 5
g 6
dtype: int64 # 如果传入axis=1,则多一个轴方向,会变成DataFrame
pd.concat([s1,s2,s3],axis=1) 0 1 2
a 0.0 NaN NaN
b 1.0 NaN NaN
c NaN 2.0 NaN
d NaN 3.0 NaN
e NaN 4.0 NaN
f NaN NaN 5.0
g NaN NaN 6.0 s4 = pd.concat([s1*5,s3])
s4 a 0
b 5
f 5
g 6
dtype: int64 # 默认合并的还是outer
pd.concat([s1,s4],axis=1) 0 1
a 0.0 0
b 1.0 5
f NaN 5
g NaN 6 # 默认合并的还是outer,如果想得到合并的交集,则指定join = 'inner'
pd.concat([s1,s4],axis=1,join='inner') 0 1
a 0 0
b 1 5 # 指定要合并的索引名,如果没有,则合并为NaN
pd.concat([s1,s4],axis=1,join_axes=[['a','b','c','e']]) 0 1
a 0.0 0.0
b 1.0 5.0
c NaN NaN
e NaN NaN #在合并行索引上创建一个层次化索引,keys参数
pd.concat([s1,s4],keys=['one','two','three']) one a 0
b 1
two a 0
b 5
f 5
g 6
dtype: int64 pd.concat([s1,s2,s3],axis=1) 0 1 2
a 0.0 NaN NaN
b 1.0 NaN NaN
c NaN 2.0 NaN
d NaN 3.0 NaN
e NaN 4.0 NaN
f NaN NaN 5.0
g NaN NaN 6.0 #如果沿着axis=1对Series进行合并,则keys就会成为DATAFrame的列名
pd.concat([s1,s2,s3],axis=1,keys=['one','two','three']) one two three
a 0.0 NaN NaN
b 1.0 NaN NaN
c NaN 2.0 NaN
d NaN 3.0 NaN
e NaN 4.0 NaN
f NaN NaN 5.0
g NaN NaN 6.0

DataFrame的concat操作

df1 = pd.DataFrame(np.arange(6).reshape(3,2),index=['a','b','c'],columns=['one','two'])
df1 one two
a 0 1
b 2 3
c 4 5 df2 = pd.DataFrame(5 + np.arange(4).reshape(2,2),index=['a','c'],columns=['three','four'])
df2 three four
a 5 6
c 7 8 # 合并列
pd.concat([df1,df2],axis=1) one two three four
a 0 1 5.0 6.0
b 2 3 NaN NaN
c 4 5 7.0 8.0 # 如果传入的不是列表而是一个字典,则字典的键就是层次化索引列名
pd.concat({'level1':df1,'level2':df2},axis=1) level1 level2
one two three four
a 0 1 5.0 6.0
b 2 3 NaN NaN
c 4 5 7.0 8.0 # names的命名是层次化索引的行标签,upper行对应level1,level2
pd.concat([df1,df2],axis=1,keys=['level1','level2'],names=['upper','lower']) upper level1 level2
lower one two three four
a 0 1 5.0 6.0
b 2 3 NaN NaN
c 4 5 7.0 8.0 df3 = pd.DataFrame(np.random.randn(3,4),columns=['a','b','c','d'])
df4 = pd.DataFrame(np.random.randn(2,3),columns=['b','d','a']) # 这样的行索引重复的难看要死,可以关闭了,ignore_index=True
pd.concat([df3,df4]) a b c d
0 0.649869 -0.332470 0.918562 -1.781167
1 -0.271012 0.702998 -2.164433 0.185556
2 0.279104 -0.846209 -0.366614 0.444451
0 -0.204010 -0.974424 NaN -2.215621
1 0.504930 0.490877 NaN 0.332790 #ingore_index启用后,行索引就会自增 pd.concat([df3,df4],ignore_index=True) a b c d
0 0.649869 -0.332470 0.918562 -1.781167
1 -0.271012 0.702998 -2.164433 0.185556
2 0.279104 -0.846209 -0.366614 0.444451
3 -0.204010 -0.974424 NaN -2.215621
4 0.504930 0.490877 NaN 0.332790

合并重叠数据(combine_first)

a = Series([np.nan,2.5,np.nan,3.5,4.5,np.nan],index=['f','e','d','c','b','a'])
a f NaN
e 2.5
d NaN
c 3.5
b 4.5
a NaN
dtype: float64 b = Series(np.arange(len(a),dtype=np.float64),index=['f','e','d','c','b','a'])
b f 0.0
e 1.0
d 2.0
c 3.0
b 4.0
a 5.0
dtype: float64 # where(条件,真值,假值),这里a数据集有null条件成立,故返回b的值
np.where(pd.isnull(a),b,a) array([0. , 2.5, 2. , 3.5, 4.5, 5. ]) a[2:]
d NaN
c 3.5
b 4.5
a NaN
dtype: float64 b[:-2]
f 0.0
e 1.0
d 2.0
c 3.0
dtype: float64 # 用a的数据填补b,如果有重复的以b为准
b[:-2].combine_first(a[2:]) a NaN
b 4.5
c 3.0
d 2.0
e 1.0
f 0.0
dtype: float64 # 用b的数据填补a,如果有重复的,以a为准
a[2:].combine_first(b[:-2]) a NaN
b 4.5
c 3.5
d 2.0
e 1.0
f 0.0
dtype: float64

Pandas合并数据集之concat、combine_first方法的更多相关文章

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

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

  2. Python数据科学手册-Pandas:合并数据集

    将不同的数据源进行合并 , 类似数据库 join merge . 工具函数 concat / append pd.concat() 简易合并 合并高维数据 默认按行合并. axis=0 ,试试 axi ...

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

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

  4. pandas(七)数据规整化:清理、转换、合并、重塑之合并数据集

    pandas对象中的数据可以通过一些内置的方式进行合并: pandas.merge 可根据一个或多个键将不同的DataFrame中的行连接起来. pandas.concat可以沿着一条轴将多个对象堆叠 ...

  5. python merge、concat合并数据集

    数据规整化:合并.清理.过滤 pandas和python标准库提供了一整套高级.灵活的.高效的核心函数和算法将数据规整化为你想要的形式! 本篇博客主要介绍: 合并数据集:.merge()..conca ...

  6. Pandas 合并 concat

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

  7. MySQL把多个字段合并成一条记录的方法

    转:http://www.111cn.net/database/mysql/71591.htm MySQL把多个字段合并成一条记录的方法 在mysql中字段合并可以使用很多函数来实现,如可以利用 GR ...

  8. JS合并两个数组的方法

    JS合并两个数组的方法 我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况.比如: var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合 ...

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

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

随机推荐

  1. mongo 索引,速度

    (如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...

  2. vim粘贴缩进问题

    vim不支持直接从其他应用复制内容粘贴过来,而是模拟用户键盘输入来实现的,一般设置vim在换行时自动以上一行的的缩进为初始位置,这样就会导致复制过来的内容出现缩进错乱. set paste 解决粘贴乱 ...

  3. java 中Math 的常用方法

    public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...

  4. Spark 性能调优-内存设置-GC设置

    http://mt.sohu.com/20150604/n414449770.shtml http://my.oschina.net/mkh/blog/330386 http://itindex.ne ...

  5. VS2010自定义添加创建者、创建时间等个人信息新建文件模版

    不知不觉VS2010已经成为.NET开发人员的必备工具,相比经典版VS2005,到过渡版vs2008,2010在性能稳定性和易用性上都得到很大的提高. 结合VS工具,其下的插件也层出不穷.今天重点给大 ...

  6. Spring Boot之执行器端点(Actuator Endpoint)实现剖析

    整体实现思路是将端点(Endpoint)适配委托给MVC层策略端点(MvcEndpoint),再通过端点MVC适配器(EndpointMvcAdapter)将端点暴露为HTTP请求方式的MVC端点,最 ...

  7. Java执行JavaScript代码

    Java执行JavaScript代码 这篇文章主要为大家详细介绍了Java执行JavaScript代码的具体操作方法,感兴趣的小伙伴们可以参考一下 我们要在Java中执行JavaScriptMetho ...

  8. sql server 索引碎片相关问题

    1.查看表的索引碎片情况 --改成当前库 use DB_Name --创建变量 指定要查看的表 declare @table_id int set @table_id=object_id('Table ...

  9. redis-缓存失效三种策略(FIFO 、LRU、LFU)

    缓存失效策略(FIFO .LRU.LFU三种算法的区别) 当缓存需要被清理时(比如空间占用已经接近临界值了),需要使用某种淘汰算法来决定清理掉哪些数据.常用的淘汰算法有下面几种: FIFO:First ...

  10. ORA-01219: 数据库未打开: 仅允许在固定表/视图中查询解决之道

    参考文章:https://blog.csdn.net/Trigl/article/details/50933495 解决.