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

最新文章

  1. c++与java中子类中调用父类成员的方法
  2. 如何在MFC界面开发中响应Button按钮的Down和Up事件
  3. 基于SURF特征的图像与视频拼接技术的研究和实现(一)
  4. Pass Dynamic Value to a Grid Label
  5. PAT乙级真题1002. 写出这个数 (20)(解题)
  6. iOS RGB颜色封装
  7. 格而知之16:我所理解的Block(2)
  8. eclipse开发工具Import工程后,工程文件夹上出现黄色感叹号——解决方法
  9. on IRC, how to use secure connection(SSL) and get a cloak/vhost to hide your IP
  10. 设n是奇数,证明:16|(n4+4n2+11)(整除原理1.1.1)
  11. 用cmd命令创建oracle 数据库、用户和表空间
  12. python eval()和exec()以及complie()
  13. CentOS 7命令行安装GNOME、KDE图形界面
  14. POJ 2871
  15. 仿饿了么增加购物车旋转控件 - 自带闪转腾挪动画 的button
  16. 吴裕雄 实战python编程(2)
  17. apache配置防盗链
  18. TCP Server有两个套接字
  19. Linux内核设计第一周学习总结 计算机如何工作
  20. UESTC 2015dp专题 A 男神的礼物 区间dp

热门文章

  1. Docker不完全使用指南
  2. csv文件的读取写法 from Udacity
  3. Subarray Product Less Than K
  4. 2019-07-30 C#基础知识学习
  5. Linux就该这么学——新手必须掌握的命令之系统状态检测命令组
  6. Mockito中的@Mock和@Spy如何使用
  7. Resource通配符路径 ——跟我学spring3
  8. vue防重复点击(指令实现)
  9. python之判断和循环
  10. web端文件上传,预览,下载,删除