pandas常用功能_2:数据合并merge

python有多种数据合并的方法,在不同的场合我们可以使用不用的方法

一、Merge

merge可能是我们最熟悉,也最常用的一个方法,因为它的用法与mysql的连接(join)非常类似。

函数介绍

pd.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,)

参数详解

【常用参数】

  • left: DataFrame | Series,合并的左表
  • right: DataFrame | Series,合并的右表
  • how: str = “inner” 合并方式,默认内连接,常用的可以改成left,左连接
  • on: IndexLabel | None = None, 关联键(如果左右表名称不一样,用下面2个参数)
  • left_on: IndexLabel | None = None, 左表关联键
  • right_on: IndexLabel | None = None,右表关联键
  • suffixes: Suffixes = (“_x”, “_y”), 左右表相同名称的变量,会自动添加后缀名

【非常用参数】

  • left_index: bool = False,
  • right_index: bool = False,
  • sort: bool = False,
  • copy: bool = True,
  • indicator: bool = False,
  • validate: str | None = None,

代码示例

创建示例数据

import pandas as pd
import numpy as np

left = pd.DataFrame({
   'key': [1, 2, 3], 'val': ['a', 'b', 'c']})
right 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值