pandas concat()

本文详细介绍了Python数据分析库Pandas中的concat()函数,包括如何使用它来合并DataFrame和Series对象,讨论了不同参数如axis、join、ignore_index等的作用,并通过实例展示了如何进行水平拼接和垂直堆叠。此外,还探讨了concat()在处理数据集合并时的性能和最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

concat()函数可以在行和列两个水平上灵活的合并多个数据框,基本用法如下:
def concat(
    objs,
    axis=0,
    join="outer",
    join_axes=None,
    ignore_index=False,
    keys=None,
    levels=None,
    names=None,
    verify_integrity=False,
    sort=None,
    copy=True,
):
    '''

    Parameters
    ----------
    objs : a sequence or mapping of Series or DataFrame objects
        If a dict is passed, the sorted keys will be used as the `keys`
        argument, unless it is passed, in which case the values will be
        selected (see below). Any None objects will be dropped silently unless
        they are all None in which case a ValueError will be raised.
    axis : {0/'index', 1/'columns'}, default 0
        The axis to concatenate along.
    join : {'inner', 'outer'}, default 'outer'
        How to handle indexes on other axis (or axes).
    join_axes : list of Index objects
        .. deprecated:: 0.25.0

        Specific indexes to use for the other n - 1 axes instead of performing
        inner/outer set logic. Use .reindex() before or after concatenation
        as a replacement.
    ignore_index : bool, default False
        If True, do not use the index values along the concatenation axis. The
        resulting axis will be labeled 0, ..., n - 1. This is useful if you are
        concatenating objects where the concatenation axis does not have
        meaningful indexing information. Note the index values on the other
        axes are still respected in the join.
    keys : sequence, default None
        If multiple levels passed, should contain tuples. Construct
        hierarchical index using the passed keys as the outermost level.
    levels : list of sequences, default None
        Specific levels (unique values) to use for constructing a
        MultiIndex. Otherwise they will be inferred from the keys.
    names : list, default None
        Names for the levels in the resulting hierarchical index.
    verify_integrity : bool, default False
        Check whether the new concatenated axis contains duplicates. This can
        be very expensive relative to the actual data concatenation.
    sort : bool, default None
        Sort non-concatenation axis if it is not already aligned when `join`
        is 'outer'. The current default of sorting is deprecated and will
        change to not-sorting in a future version of pandas.

        Explicitly pass ``sort=True`` to silence the warning and sort.
        Explicitly pass ``sort=False`` to silence the warning and not sort.

        This has no effect when ``join='inner'``, which already preserves
        the order of the non-concatenation axis.

        .. versionadded:: 0.23.0

    copy : bool, default True
        If False, do not copy data unnecessarily.

    Returns
    -------
    object, type of objs
        When concatenating all ``Series`` along the index (axis=0), a
        ``Series`` is returned. When ``objs`` contains at least one
        ``DataFrame``, a ``DataFrame`` is returned. When concatenating along
        the columns (axis=1), a ``DataFrame`` is returned.
        '''
	>>> df1 = pd.DataFrame([['a', 1], ['b', 2]],columns=['letter', 'number'])
    >>> df1
      letter  number
    0      a       1
    1      b       2
    >>> df2 = pd.DataFrame([['c', 3], ['d', 4]],columns=['letters', 'number'])
    >>> df2
      letters  number
0       c       3
1       d       4
   #按行拼接,相同列名合并拼接,不相同的补na
   #Combine ``DataFrame`` objects with overlapping columns and return everything.Columns outside the intersection will be filled with ``NaN`` values.
   #axis默认=0,就是累加行
    >>> pd.concat([df1, df2])
       letter letters  number
0      a     NaN       1
1      b     NaN       2
0    NaN       c       3
1    NaN       d       4
>>> pd.concat([df1, df2],axis=1)
	letter  number letters  number
0      a       1       c       3
1      b       2       d       4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值