• pd.merge( df1, df2, on=['key1', 'key2'], left_index=True, right_index=True, how=['left', 'right', 'outer', 'inner'], indicator='indicator_column', suffixes=['_boy', '_girl'] )
from __future__ import print_function
import pandas as pd

merging two df by key/keys, on='key'. (may be used in database)

# merging two df by key/keys. (may be used in database)
# simple example
left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
print(left)
print(right)
res = pd.merge(left, right, on='key') # 基于列标签为‘key’合并
print(res) > key A B
> 0 K0 A0 B0
> 1 K1 A1 B1
> 2 K2 A2 B2
> 3 K3 A3 B3 > key C D
> 0 K0 C0 D0
> 1 K1 C1 D1
> 2 K2 C2 D2
> 3 K3 C3 D3 > key A B C D
> 0 K0 A0 B0 C0 D0
> 1 K1 A1 B1 C1 D1
> 2 K2 A2 B2 C2 D2
> 3 K3 A3 B3 C3 D3

consider two keys, on=['key1', 'key2']

# consider two keys
left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
'key2': ['K0', 'K1', 'K0', 'K1'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
'key2': ['K0', 'K0', 'K0', 'K0'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
print(left)
print(right) > key1 key2 A B
> 0 K0 K0 A0 B0
> 1 K0 K1 A1 B1
> 2 K1 K0 A2 B2
> 3 K2 K1 A3 B3 > key1 key2 C D
> 0 K0 K0 C0 D0
> 1 K1 K0 C1 D1
> 2 K1 K0 C2 D2
> 3 K2 K0 C3 D3
res = pd.merge(left, right, on=['key1', 'key2'], how='inner')  # default for how='inner'
print(res) # 求交集
res = pd.merge(left, right, on=['key1', 'key2'], how='outer') # default for how='inner'
print(res) # 求并集 > key1 key2 A B C D
> 0 K0 K0 A0 B0 C0 D0
> 1 K1 K0 A2 B2 C1 D1
> 2 K1 K0 A2 B2 C2 D2 > key1 key2 A B C D
> 0 K0 K0 A0 B0 C0 D0
> 1 K0 K1 A1 B1 NaN NaN
> 2 K1 K0 A2 B2 C1 D1
> 3 K1 K0 A2 B2 C2 D2
> 4 K2 K1 A3 B3 NaN NaN
> 5 K2 K0 NaN NaN C3 D3

how = ['left', 'right', 'outer', 'inner']

# how = ['left', 'right', 'outer', 'inner']
res = pd.merge(left, right, on=['key1', 'key2'], how='right')
print(res) # 以右边的数据为标准,来合并 > key1 key2 A B C D
> 0 K0 K0 A0 B0 C0 D0
> 1 K1 K0 A2 B2 C1 D1
> 2 K1 K0 A2 B2 C2 D2
> 3 K2 K0 NaN NaN C3 D3

显示数据的来源,多_merge这一列,默认是false不显示:indicator=True 或 indicator='indicator_column' 自定义indicator列名称

# indicator
df1 = pd.DataFrame({'col1':[0,1], 'col_left':['a','b']})
df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]}) print(df1)
print(df2)
res = pd.merge(df1, df2, on='col1', how='outer', indicator=True)
print(res) > col1 col_left
> 0 0 a
> 1 1 b > col1 col_right
> 0 1 2
> 1 2 2
> 2 2 2 > col1 col_left col_right _merge
> 0 0 a NaN left_only
> 1 1 b 2.0 both
> 2 2 NaN 2.0 right_only
> 3 2 NaN 2.0 right_only
# give the indicator a custom name
res = pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_column')
print(res) # 设置这列的标题为'indicator_column' > col1 col_left col_right indicator_column
> 0 0 a NaN left_only
> 1 1 b 2.0 both
> 2 2 NaN 2.0 right_only
> 3 2 NaN 2.0 right_only

handle overlapping 信息重叠: suffixes=['_boy', '_girl']

# handle overlapping
boys = pd.DataFrame({'k': ['K0', 'K1', 'K2'], 'age': [1, 2, 3]})
girls = pd.DataFrame({'k': ['K0', 'K0', 'K3'], 'age': [4, 5, 6]})
res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='inner') print(boys)
print(girls)
print(res) > k age
> 0 K0 1
> 1 K1 2
> 2 K2 3 > k age
> 0 K0 4
> 1 K0 5
> 2 K3 6 > k age_boy age_girl
> 0 K0 1 4
> 1 K0 1 5
res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='outer')
print(res) # 这里的K0,K1理解成姓名,同一个姓名对应了不同的年龄,说明信息重叠了。 > k age_boy age_girl
> 0 K0 1.0 4.0
> 1 K0 1.0 5.0
> 2 K1 2.0 NaN
> 3 K2 3.0 NaN
> 4 K3 NaN 6.0

merged by index: left_index=True, right_index=True

# merged by index
left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
'B': ['B0', 'B1', 'B2']},
index=['K0', 'K1', 'K2'])
right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],
'D': ['D0', 'D2', 'D3']},
index=['K0', 'K2', 'K3'])
print(left)
print(right) > A B
> K0 A0 B0
> K1 A1 B1
> K2 A2 B2 > C D
> K0 C0 D0
> K2 C2 D2
> K3 C3 D3 # left_index and right_index 默认是False
res = pd.merge(left, right, left_index=True, right_index=True, how='outer')
print(res) # 并
res = pd.merge(left, right, left_index=True, right_index=True, how='inner')
print(res) # 交 > A B C D
> K0 A0 B0 C0 D0
> K1 A1 B1 NaN NaN
> K2 A2 B2 C2 D2
> K3 NaN NaN C3 D3 > A B C D
> K0 A0 B0 C0 D0
> K2 A2 B2 C2 D2

join:join function in pandas is similar with merge. If know merge, you will understand join

END

pandas 7 合并 merge 水平合并,数据会变宽的更多相关文章

  1. R语言中的横向数据合并merge及纵向数据合并rbind的使用

    R语言中的横向数据合并merge及纵向数据合并rbind的使用 我们经常会遇到两个数据框拥有相同的时间或观测值,但这些列却不尽相同.处理的办法就是使用merge(x, y ,by.x = ,by.y ...

  2. Pandas 合并merge

    pandas中的merge和concat类似,但主要是用于两组有key column的数据,统一索引的数据. 通常也被用在Database的处理当中. 1.依据一组key合并 >>> ...

  3. 【转】Pandas学习笔记(六)合并 merge

    Pandas学习笔记系列: Pandas学习笔记(一)基本介绍 Pandas学习笔记(二)选择数据 Pandas学习笔记(三)修改&添加值 Pandas学习笔记(四)处理丢失值 Pandas学 ...

  4. pandas之DataFrame合并merge

    一.merge merge操作实现两个DataFrame之间的合并,类似于sql两个表之间的关联查询.merge的使用方法及参数解释如下: pd.merge(left, right, on=None, ...

  5. 动态横向(水平)合并GridView数据行DataRow的列

    前一段时间,Insus.NET有写过<动态合并GridView数据行DataRow的列>http://www.cnblogs.com/insus/p/3238348.html, 那是纵向( ...

  6. 动态横向(水平)合并Repeater数据行DataItem的列

    Insus.NET有对GridView控件进行横纵分别合并列:横:<动态横向(水平)合并GridView数据行DataRow的列>http://www.cnblogs.com/insus/ ...

  7. 【转载】C#的Merge方法合并两个DataTable对象的数据

    在C#中的Datatable类中,可以使用DataTable类的Merge方法对两个相同结构的DataTable对象进行求并集运算,将两个DataTable对象的数据行合并到其中一个DataTable ...

  8. Lucene学习总结之五:Lucene段合并(merge)过程分析

    一.段合并过程总论 IndexWriter中与段合并有关的成员变量有: HashSet<SegmentInfo> mergingSegments = new HashSet<Segm ...

  9. C++ Opencv split()通道分离函数 merge()通道合并函数 使用操作详解

    一. split()通道分离函数 split()函数的C++版本有两个原型,他们分别是: C++: void split(const Mat& src, Mat*mvbegin);//& ...

随机推荐

  1. adb屏幕截屏

    import subprocess #执行结果使用管道输出,对于参数是字符串,需要指定shell=Trueprocess = subprocess.Popen('adb shell screencap ...

  2. 多个账号GitHub账号配置

    1.vi config 重复以上步骤就行 然后#注释下  是个人账号还是公司用的账号 mv id_rsa   id_rsa_qq   做下区别,防止冲突 ,别忘了,路径也要改下 mv   id_rsa ...

  3. STM32 USB转串口驱动安装不成功出现黄色感叹号解决方法!

    相信很多人在做USB转串口时出现过串口驱动安装不成功,出现黄色感叹号问题, 出现这种问题一般是驱动安装不成功造成的. 这里我就这个问题总结几个简单的方法. 方法1: 插上USB,利用驱动人生安装驱动. ...

  4. Js原生实现抽奖功能

    <div>代码 按钮代码 JS原生代码  完整的代码: <div style="width:365px;height:300px;border:2px solid gree ...

  5. Sublime 插件Pylinter could not automatically determined the path to lint.py

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50618630 安装Sublime Te ...

  6. eclipse project文件夹下 删除不掉文件夹或者文件的解决的方法

    对于新手来说,有时操作失误就会导致eclipse文件夹中的某些子文件夹或者文件无法删除. 这种原因是,在project文件夹中(不是eclipse上显示的.是真实的物理磁盘上的)这个文件夹或者文件已经 ...

  7. [Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular

    By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for commu ...

  8. linux openssl加密文件

    openssl 支持的加密算法 -aes-128-cbc -aes-128-cfb -aes-128-cfb1 -aes-128-cfb8 -aes-128-ecb -aes-128-ofb -aes ...

  9. Restful技术

    一.概述 Restful技术是一种架构风格(Representational State Transfer)表现层状态转化,而不是一种编程标准. 之前前后端混在一起,前端通过mapping映射找到后端 ...

  10. 换今日特价图片---轻开电子商务系统(企业入门级B2C站点)

    跟换主页轮播图片一样,一共4个文件: 列表显示文件:site/links/img2.html 加入图片文件:site/links/img2_add.html 加入保存图片文件:site/links/i ...