Pandas中DataFrame数据合并、连接(concat、merge、join)之merge
二、merge:通过键拼接列
类似于关系型数据库的连接方式,可以根据一个或多个键将不同的DatFrame连接起来。
该函数的典型应用场景是,针对同一个主键存在两张不同字段的表,根据主键整合到一张表里面。
merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=True,
suffixes=('_x', '_y'), copy=True, indicator=False)
参数介绍:
left和right:两个不同的DataFrame;
how:连接方式,有inner、left、right、outer,默认为inner;
on:指的是用于连接的列索引名称,必须存在于左右两个DataFrame中,如果没有指定且其他参数也没有指定,则以两个DataFrame列名交集作为连接键;
left_on:左侧DataFrame中用于连接键的列名,这个参数左右列名不同但代表的含义相同时非常的有用;
right_on:右侧DataFrame中用于连接键的列名;
left_index:使用左侧DataFrame中的行索引作为连接键;
right_index:使用右侧DataFrame中的行索引作为连接键;
sort:默认为True,将合并的数据进行排序,设置为False可以提高性能;
suffixes:字符串值组成的元组,用于指定当左右DataFrame存在相同列名时在列名后面附加的后缀名称,默认为('_x', '_y');
copy:默认为True,总是将数据复制到数据结构中,设置为False可以提高性能;
indicator:显示合并数据中数据的来源情况
案例1
import pandas as pd
import numpy as np
random = np.random.RandomState(0) #随机数种子,相同种子下每次运行生成的随机数相同
df1=pd.DataFrame(random.randn(3,4),columns=['a','b','c','d'])
df1

random = np.random.RandomState(0)
df2=pd.DataFrame(random.randn(2,3),columns=['b','d','a'],index=["a1","a2"])
df2

pd.merge(df1,df2)

pd.merge(df1,df2,how="outer")

案例2 默认按相同的列名键join
df3=pd.DataFrame({'key':['a','b','b'],'data1':range(3)})
df3

df4=pd.DataFrame({'key':['a','b','c'],'data2':range(3)})
df4

pd.merge(df3,df4)

案例三
df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'],
'value': [1, 2, 3, 5]})
df1

df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'],
'value': [5, 6, 7, 8]})
df2

df1.merge(df2)

Merge df1 and df2 on the lkey and rkey columns. The value columns have the default suffixes, _x and _y,
df1.merge(df2,left_on="lkey",right_on="rkey")

>>> df1.merge(df2, left_on='lkey', right_on='rkey', suffixes=(False, False))
Traceback (most recent call last):
...
ValueError: columns overlap but no suffix specified:
Index(['value'], dtype='object')
Pandas中DataFrame数据合并、连接(concat、merge、join)之merge的更多相关文章
- Pandas中DataFrame数据合并、连接(concat、merge、join)之concat
一.concat:沿着一条轴,将多个对象堆叠到一起 concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, key ...
- Pandas中DataFrame数据合并、连接(concat、merge、join)之join
pandas.DataFrame.join 自己弄了很久,一看官网.感觉自己宛如智障.不要脸了,直接抄 DataFrame.join(other, on=None, how='left', lsuff ...
- Spark与Pandas中DataFrame对比
Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...
- Spark与Pandas中DataFrame对比(详细)
Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...
- 将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy
将pandas的DataFrame数据写入MySQL数据库 + sqlalchemy import pandas as pd from sqlalchemy import create_engine ...
- Python3 Pandas的DataFrame数据的增、删、改、查
Python3 Pandas的DataFrame数据的增.删.改.查 一.DataFrame数据准备 增.删.改.查的方法有很多很多种,这里只展示出常用的几种. 参数inplace默认为False,只 ...
- Pandas中DataFrame修改列名
Pandas中DataFrame修改列名:使用 rename df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01- ...
- pandas中DataFrame的ix,loc,iloc索引方式的异同
pandas中DataFrame的ix,loc,iloc索引方式的异同 1.loc: 按照标签索引,范围包括start和end 2.iloc: 在位置上进行索引,不包括end 3.ix: 先在inde ...
- pandas中,dataframe 进行数据合并-pd.concat()
``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...
随机推荐
- #Java第三周总结
第三周实验题目+总结 第一题:打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数 ...
- interface和struct
interface: C++关键字中没有interface,即接口.interface和class不同,interface仅有接口声明,而且所有声明默认的访问权限是public而非private. C ...
- PAT A1031 Hello World for U (20)
思路: 读取数组 int i = 0; while(cin >> word) { c[i] = word; i++; } 计算边长 int n1 = (length + 2) / 3; i ...
- 2 - sat 模板(自用)
2-sat一个变量两种状态符合条件的状态建边找强连通,两两成立1 - n 为第一状态(n + 1) - (n + n) 为第二状态 例题模板 链接一 POJ 3207 Ikki's Story IV ...
- redis 学习(20)-- 常见的持久化开发与运维问题
常见的持久化开发与运维问题 fork 操作 fork 操作是一个同步操作,若执行较慢会阻塞 redis 主线程 执行时间与内存量相关:内存越大,耗时越长:虚拟机较慢,真机较快 查看 fork 执行时间 ...
- git的常用指令(一)
1. 查看git远程的所有分支 git branch -a 2.查看本地已有的分支 git branch 3.本地检出一个新的分支并推送到远程仓库 一).创建本地分支 git checkout -b ...
- IIS Express启动不了的的解决方案
netsh http show iplisten netsh http delete iplisten ipaddress=11.22.33.44 (where 11.22.33.44 is the ...
- 【原创】大数据基础之Drill(1)简介、安装及使用
https://drill.apache.org/ 一 简介 Drill is an Apache open-source SQL query engine for Big Data explorat ...
- centos7上的firewalld 的使用
#centos7上的firewalld 的使用 一.firewalld的基本启动关闭命令 启动服务------systemctl start firewalld 关闭服务------systemctl ...
- 高德地图API-设置考勤范围
<template> <div class="page-setting-setgps"> <!--head--> <div class=& ...