concat,merge表格的拼接,concatenate数组的拼接以及数组添加列和行

1.merge函数拼接

  • merge(left, right, how=‘inner’, on=None, left_on=None, right_on=None,
    left_index=False, right_index=False, sort=True,
    suffixes=(’_x’, ‘_y’), copy=True, indicator=False)

  • how:指的是合并(连接)的方式有inner(内连接),采用内连接,连接两边都有的值left(左外连接)取左表所有数据,right(右外连接),outer(全外连接)outer外连接时,会取并集,并用NaN填充;默认为inner

  • on : 指的是用于连接的列索引名称。必须存在右右两个DataFrame对象中,如果没有指定且其他参数也未指定则以两个DataFrame的列名交集做为连接键

  • left_on:左则DataFrame中用作连接键的列名;这个参数中左右列名不相同,但代表的含义相同时非常有用。

  • right_on:右则DataFrame中用作 连接键的列名

  • left_index:使用左则DataFrame中的行索引做为连接键

  • right_index:使用右则DataFrame中的行索引做为连接键

  • sort:默认为True,将合并的数据进行排序。在大多数情况下设置为False可以提高性能

  • suffixes:字符串值组成的元组,用于指定当左右DataFrame存在相同列名时在列名后面附加的后缀名称,默认为(’_x’,’_y’)

  • copy:默认为True,总是将数据复制到数据结构中;大多数情况下设置为False可以提高性能

  • indicator:在 0.17.0中还增加了一个显示合并数据中来源情况;如只来自己于左边(left_only)、两者(both)
    在这里插入图片描述

  • a表和b表相同列的索引名称不同,可以通过指定left_on,right_on来进行合并

  • left_on 左表的索引值 即:b,right_on右表索引值 即:a

  • indicator 显示数据来源 both 公有的

c = pd.merge(b,a,left_on='b',right_on='e',indicator=True)
c

在这里插入图片描述
在这里插入图片描述

  • 有共同列并且列索引相同 on = ’ b ’
c = pd.merge(b,a,on='b',sort=False,indicator=True)
c

在这里插入图片描述

  • 索引名称进行连接
c = pd.merge(b,a,left_index=True,right_index=True,sort=False,indicator=True)
c

2.concat函数拼接

  • pd.concat(
    objs: Union[Iterable[ForwardRef(‘NDFrame’)], Mapping[Union[Hashable, NoneType], ForwardRef(‘NDFrame’)]],
    axis=0,
    join=‘outer’,
    ignore_index: bool = False,
    keys=None,
    levels=None,
    names=None,
    verify_integrity: bool = False,
    sort: bool = False,
    copy: bool = True,
    )

  • axis=0 行拼接 axis=1 列拼接

  • ignore_index = True 合并后重新索引

  • join = ‘inner’ 内连接取交集 (交集为含有列索引名称相同的合并) ‘outer’ 外连接取并集(如果不存在时自动填充为nan)

  • keys 说明数据的来源

  • sort 排序
    在这里插入图片描述

  • 1.以列进行左右连接

c = pd.concat([a,b],axis=1,keys=['a','b'],names=['ll','kk'])
c

在这里插入图片描述

  • 2.取并集,
c = pd.concat([a,b],join='outer')
c

在这里插入图片描述

3.numpy中concatenate()函数拼接

a = np.array([[11, 22], [33, 44]])
a
>> array([[11, 22],
       [33, 44]])
b = np.array([[55, 66]])
b
>> array([[55, 66]])
  • 俩个数组上下拼接
np.concatenate((a, b), axis=0)
array([[11, 22],
       [33, 44],
       [55, 66]])
  • 俩个数组左右拼接(把b表先转置)
b.T
array([[55],
       [66]])
np.concatenate((a, b.T), axis=1)
array([[11, 22, 55],
       [33, 44, 66]])
  • 每一个元素增加[]
a[:,np.newaxis]
array([[[11, 22]],

       [[33, 44]]])
  • 给最外层增加一个[]
a[np.newaxis,:]
array([[[11, 22],
        [33, 44]]])
  • 数组最后一列添加一列
c = np.array([[11,22,33],[44,55,66],[77,88,99]])
c
array([[11, 22, 33],
       [44, 55, 66],
       [77, 88, 99]])
       

c = np.column_stack((c,np.arange(1,4)))
c
array([[11, 22, 33,  1],
       [44, 55, 66,  2],
       [77, 88, 99,  3]])
  • 数组最后一行添加一行
c = np.row_stack((c,np.arange(1,4)))
c
array([[11, 22, 33],
       [44, 55, 66],
       [77, 88, 99],
       [ 1,  2,  3]])
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最低调的奢华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值