• pandas的连接函数concat()函数


     
    1.  
      pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
    2.  
      keys=None, levels=None, names=None, verify_integrity=False,
    3.  
      copy=True)

    参数含义

    • objs:Series,DataFrame或Panel对象的序列或映射。如果传递了dict,则排序的键将用作参数,除非它被传递,在这种情况下,将选择值(见下文)。任何无对象将被静默删除,除非它们都是无,在这种情况下将引发一个ValueError。
    • axis:{0,1,...},默认为0。沿着连接的轴。
    • join:{'inner','outer'},默认为“outer”。如何处理其他轴上的索引。outer为联合和inner为交集。
    • ignore_index:boolean,default False。如果为True,请不要使用并置轴上的索引值。结果轴将被标记为0,...,n-1。如果要连接其中并置轴没有有意义的索引信息的对象,这将非常有用。注意,其他轴上的索引值在连接中仍然受到尊重。
    • join_axes:Index对象列表。用于其他n-1轴的特定索引,而不是执行内部/外部设置逻辑。
    • keys:序列,默认值无。使用传递的键作为最外层构建层次索引。如果为多索引,应该使用元组。
    • levels:序列列表,默认值无。用于构建MultiIndex的特定级别(唯一值)。否则,它们将从键推断。
    • names:list,default无。结果层次索引中的级别的名称。
    • verify_integrity:boolean,default False。检查新连接的轴是否包含重复项。这相对于实际的数据串联可能是非常昂贵的。
    • copy:boolean,default True。如果为False,请勿不必要地复制数据。

     
    import numpy as np
    import pandas as pd
    from pandas import Series,DataFrame

    一、Numpy数组的连接–concatenate

    a = np.arange(6).reshape(2,3)
    a
    array([[0, 1, 2],
           [3, 4, 5]])
    

    1.垂直连接

    np.concatenate([a,a])
    array([[0, 1, 2],
           [3, 4, 5],
           [0, 1, 2],
           [3, 4, 5]])
    

    2.水平连接

    np.concatenate([a,a],axis=1)
    array([[0, 1, 2, 0, 1, 2],
           [3, 4, 5, 3, 4, 5]])
    

    二、pandas.concat函数

    s1 = Series([0,1],index = ['a','b'])
    s2 = Series([2,3,4],index = ['c','d','e'])
    s3 = Series([5,6],index = ['f','g'])

    1.垂直连接

    pd.concat([s1,s2,s3])
    a    0
    b    1
    c    2
    d    3
    e    4
    f    5
    g    6
    dtype: int64
    

    2.水平连接

    print(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
    

    3.对连接的对象添加索引(实现层次索引)

    print(pd.concat([s1,s2,s3],keys=['one','two','thress']))
    one     a    0
            b    1
    two     c    2
            d    3
            e    4
    thress  f    5
            g    6
    dtype: int64
    

    4.水平连接时,为结果的DataFrame添加列索引

    print(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
    

    5.对DataFrame同样适用

  • 相关阅读:
    September 17th 2016 Week 38th Saturday
    【2016-09-16】UbuntuServer14.04或更高版本安装问题记录
    September 16th 2016 Week 38th Friday
    September 11th 2016 Week 38th Sunday
    September 12th 2016 Week 38th Monday
    September 10th 2016 Week 37th Saturday
    September 9th 2016 Week 37th Friday
    c++暂停
    八皇后问题
    ( 转转)Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)
  • 原文地址:https://www.cnblogs.com/zknublx/p/9645834.html
Copyright © 2020-2023  润新知