• pandas合并


    concat

    Concatenate pandas objects along a particular axis.

    Allows optional set logic along the other axes.

    Can also add a layer of hierarchical indexing on the concatenation
    axis, which may be useful if the labels are the same (or overlapping)
    on the passed axis number.
    pandas.concat

    pandas.concat(objs, *, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
    
    • 1

    Merge vertically by default
    在这里插入图片描述

    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    b = pd.DataFrame(np.random.normal(0, 1, (5, 5)))
    c = pd.concat([a, b], axis=1, join='inner', ignore_index=True)
    
    • 1
    • 2
    • 3

    a:
    在这里插入图片描述
    b:
    在这里插入图片描述
    c:
    在这里插入图片描述

    merge

    Merge DataFrame or named Series objects with a database-style join.

    A named Series object is treated as a DataFrame with a single named
    column.

    The join is done on columns or indexes. If joining columns on columns,
    the DataFrame indexes will be ignored. Otherwise if joining indexes on
    indexes or indexes on a column or columns, the index will be passed
    on. When performing a cross merge, no column specifications to merge
    on are allowed.
    pandas.merge

    pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)
    
    • 1

    You can use Pandas merge to implement vlookup like functions

    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    a['merge'] = [1, 2, 3]
    b = pd.DataFrame(np.random.normal(0, 1, (5, 5)), index=[2, 9, 10, 11, 12])
    b['merge'] = [2, 3, 4, 5, 6]
    c = pd.merge(a, b, how='left', on='merge', suffixes=('_a', '_b'))
    
    • 1
    • 2
    • 3
    • 4
    • 5

    a:
    在这里插入图片描述
    b:
    在这里插入图片描述
    c:
    在这里插入图片描述

    join

    Join columns of another DataFrame.

    Join columns with other DataFrame either on index or on a key column.
    Efficiently join multiple DataFrame objects by index at once by
    passing a list.
    pandas.DataFrame.join

    DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False, validate=None)
    
    • 1

    joins index-on-index by default

    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    a['merge'] = [1, 2, 3]
    b = pd.DataFrame(np.random.normal(0, 1, (5, 5)), index=[2, 9, 10, 11, 12])
    b['merge'] = [2, 3, 4, 5, 6]
    c = a.join(b,  rsuffix='_b')
    
    • 1
    • 2
    • 3
    • 4
    • 5

    a:
    在这里插入图片描述
    b:
    在这里插入图片描述
    c:
    在这里插入图片描述

  • 相关阅读:
    STM32 - 笔记4
    1-k8s1.23.6-底座搭建-基于docker
    【SpringBoot】Spring Boot 实现接口的幂等性
    Apache SSI 远程命令执行漏洞复现
    Go 语言操作 MongoDb
    机器人过程自动化(RPA)入门 5. 处理应用程序中的控件
    AD四层板总结
    关于华为原生鸿蒙生态的问题!!
    WPF 使用动画绘制一个点赞大拇指
    AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
  • 原文地址:https://blog.csdn.net/sinat_28916141/article/details/127724621