pandas.DataFrame.join

自己弄了很久,一看官网。感觉自己宛如智障。不要脸了,直接抄

DataFrame.join(otheron=Nonehow='left'lsuffix=''rsuffix=''sort=False)

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.

Parameters:

other : DataFrame, Series with name field set, or list of DataFrame

Index should be similar to one of the columns in this one. If a Series is passed, its name attribute must be set, and that will be used as the column name in the resulting joined DataFrame

on : column name, tuple/list of column names, or array-like

Column(s) in the caller to join on the index in other, otherwise joins index-on-index. If multiples columns given, the passed DataFrame must have a MultiIndex. Can pass an array as the join key if not already contained in the calling DataFrame. Like an Excel VLOOKUP operation

how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default: ‘left’

How to handle the operation of the two objects.

  • left: use calling frame’s index (or column if on is specified)

  • right: use other frame’s index

  • outer: form union of calling frame’s index (or column if on is

    specified) with other frame’s index

  • inner: form intersection of calling frame’s index (or column if

    on is specified) with other frame’s index

lsuffix : string

Suffix to use from left frame’s overlapping columns

rsuffix : string

Suffix to use from right frame’s overlapping columns

sort : boolean, default False

Order result DataFrame lexicographically by the join key. If False, preserves the index order of the calling (left) DataFrame

Returns:

joined : DataFrame

See also

DataFrame.merge
For column(s)-on-columns(s) operations

Notes

on, lsuffix, and rsuffix options are not supported when passing a list of DataFrame objects

Examples

>>> caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
>>> caller
A key
0 A0 K0
1 A1 K1
2 A2 K2
3 A3 K3
4 A4 K4
5 A5 K5
>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
... 'B': ['B0', 'B1', 'B2']})
>>> other
B key
0 B0 K0
1 B1 K1
2 B2 K2

Join DataFrames using their indexes.==》join on indexes

>>> caller.join(other, lsuffix='_caller', rsuffix='_other')
>>>     A key_caller    B key_other
0 A0 K0 B0 K0
1 A1 K1 B1 K1
2 A2 K2 B2 K2
3 A3 K3 NaN NaN
4 A4 K4 NaN NaN
5 A5 K5 NaN NaN

If we want to join using the key columns, we need to set key to be the index in both caller and other. The joined DataFrame will have key as its index.

>>> caller.set_index('key').join(other.set_index('key'))
>>>      A    B
key
K0 A0 B0
K1 A1 B1
K2 A2 B2
K3 A3 NaN
K4 A4 NaN
K5 A5 NaN

Another option to join using the key columns is to use the on parameter. DataFrame.join always uses other’s index but we can use any column in the caller. This method preserves the original caller’s index in the result.

>>> caller.join(other.set_index('key'), on='key')
>>>     A key    B
0 A0 K0 B0
1 A1 K1 B1
2 A2 K2 B2
3 A3 K3 NaN
4 A4 K4 NaN
5 A5 K5 NaN

Pandas中DataFrame数据合并、连接(concat、merge、join)之join的更多相关文章

  1. Pandas中DataFrame数据合并、连接(concat、merge、join)之concat

    一.concat:沿着一条轴,将多个对象堆叠到一起 concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, key ...

  2. Pandas中DataFrame数据合并、连接(concat、merge、join)之merge

    二.merge:通过键拼接列 类似于关系型数据库的连接方式,可以根据一个或多个键将不同的DatFrame连接起来. 该函数的典型应用场景是,针对同一个主键存在两张不同字段的表,根据主键整合到一张表里面 ...

  3. Python基础 | pandas中dataframe的整合与形变(merge & reshape)

    目录 行的union pd.concat df.append 列的join pd.concat pd.merge df.join 行列转置 pivot stack & unstack melt ...

  4. Spark与Pandas中DataFrame对比

      Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...

  5. Spark与Pandas中DataFrame对比(详细)

      Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...

  6. 将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy

    将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy import pandas as pd from sqlalchemy import create_engine ...

  7. 排序合并连接(sort merge join)的原理

    排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理     排序合并连接(sort merge join)       访问次数:两张表都只会访 ...

  8. Python3 Pandas的DataFrame数据的增、删、改、查

    Python3 Pandas的DataFrame数据的增.删.改.查 一.DataFrame数据准备 增.删.改.查的方法有很多很多种,这里只展示出常用的几种. 参数inplace默认为False,只 ...

  9. Pandas中DataFrame修改列名

    Pandas中DataFrame修改列名:使用 rename df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01- ...

随机推荐

  1. [转帖]为何 linux 要用 tar.gz,而不用 7z 或 zip?

    为何 linux 要用 tar.gz,而不用 7z 或 zip? http://embeddedlinux.org.cn/emb-linux/entry-level/201908/13-8776.ht ...

  2. 利用PLSQL Developer对oracle中的数据进行备份恢复

    --以备份scott用户为例,目标(备份里面所有的对象)--切换到scottconn scott/tiger;--进入  工具-->导出用户对象  如图所示 创建表空间及用户名,并赋予权限 -- ...

  3. Spring实战(十三)Spring事务

    1.什么是事务(Transaction)? 事务是指逻辑上的一组操作,要么全部成功,要么全部失败. 事务是指将一系列数据操作捆绑成为一个整体进行统一管理.如果某一事务执行成功,则该事务中进行的所有数据 ...

  4. WPF 遍历资源字典中的控件

    object obItem=this.FindResource("canvasdt"); if (obItem is System.Windows.DataTemplate) { ...

  5. centos 配置rsync+inotify数据实时同步2

    一.Rsync服务简介 1. 什么是Rsync 它是一个远程数据同步工具,它在同步文件的同时,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“rsync算法”来使本地和远程两个主机 ...

  6. springcloud(十二)-springcloud-config统一管理微服务配置

    1.为什么要统一管理微服务配置 对于传统的单体应用,常使用配置文件管理所有配置.例如一个SpringBoot开发的单体应用,可将配置内容放在application.yml文件中.如果需要切换环境,可设 ...

  7. 【Git的基本操作三】基本操作命令

    基本操作 (1) 状态查看操作 git status 作用:查看工作区.暂存区状态 (2) 添加操作 git add [filename] 作用:将工作区文件的 添加/修改,添加到暂存区 (3) 提交 ...

  8. 第一章 PHP mySQL

    一,服务器环境搭建 1-1.Apache服务器.(端口号定义,http协议,开启和关闭) 服务器: 我们在浏览器浏览网页的时候,在地址栏中都会产生一个url.通过这个url,浏览器从互联网中找到一个网 ...

  9. Nginx如何配置防盗链

    配置要点 none : 允许没有http_refer的请求访问资源: blocked : 允许不是http://开头的,不带协议的请求访问资源: 119.28.190.215 start.igrow. ...

  10. maven入门--part2 安装

    Maven安装和配置 (1)下载安装文件apache-maven-3.03-bin.tar (2)解压至安装目录,安装完毕 (3)修改.bash_profile,修改maven安装路径,修改构建GC配 ...