Pandas的拼接操作
pandas的拼接操作
pandas的拼接分为两种:
- 级联:pd.concat, pd.append
- 合并:pd.merge, pd.join
import pandas as pd
import numpy as np
from pandas import DataFrame,Series
一. 使用pd.concat()级联
pandas使用pd.concat函数,与np.concatenate函数类似,只是多了一些参数:
objs
axis=0
keys
join='outer' / 'inner':表示的是级联的方式,outer会将所有的项进行级联(忽略匹配和不匹配),而inner只会将匹配的项级联到一起,不匹配的不级联
ignore_index=False
1)匹配级联
行列索引均一致
df1 = DataFrame(data=np.random.randint(0,100,size=(3,4)))
df1
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | 61 | 89 | 68 | 51 |
| 1 | 46 | 79 | 1 | 55 |
| 2 | 52 | 4 | 72 | 18 |
df2 = DataFrame(data=np.random.randint(0,100,size=(3,4)))
df2
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | 15 | 62 | 20 | 78 |
| 1 | 60 | 79 | 70 | 58 |
| 2 | 71 | 87 | 20 | 95 |
pd.concat((df1,df2),axis=0) # axis=0表示Y轴级联
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | 61 | 89 | 68 | 51 |
| 1 | 46 | 79 | 1 | 55 |
| 2 | 52 | 4 | 72 | 18 |
| 0 | 15 | 62 | 20 | 78 |
| 1 | 60 | 79 | 70 | 58 |
| 2 | 71 | 87 | 20 | 95 |
2) 不匹配级联
不匹配指的是级联的维度的索引不一致。例如纵向级联时列索引不一致,横向级联时行索引不一致
有2种连接方式:
外连接:补NaN(默认模式)
内连接:只连接匹配的项
df1 = DataFrame(data=np.random.randint(0,100,size=(3,4)))
df2 = DataFrame(data=np.random.randint(0,100,size=(3,3)))
pd.concat((df1,df2),axis=0)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0 | 55 | 61 | 54 | 56.0 |
| 1 | 10 | 14 | 6 | 62.0 |
| 2 | 39 | 27 | 99 | 81.0 |
| 0 | 31 | 49 | 80 | NaN |
| 1 | 73 | 42 | 44 | NaN |
| 2 | 67 | 68 | 97 | NaN |
pd.concat((df1,df2),axis=0,join='inner') # inner内连接,只级联匹配的项
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 55 | 61 | 54 |
| 1 | 10 | 14 | 6 |
| 2 | 39 | 27 | 99 |
| 0 | 31 | 49 | 80 |
| 1 | 73 | 42 | 44 |
| 2 | 67 | 68 | 97 |
二. 使用pd.merge()合并
merge与concat的区别在于,merge需要依据某一共同的列来进行合并
使用pd.merge()合并时,会自动根据两者相同column名称的那一列,作为key来进行合并。
注意每一列元素的顺序不要求一致
参数:
how:outer取并集(外连接) inner取交集(内连接)
on:当有多列相同的时候,可以使用on来指定使用那一列进行合并,on的值为一个列表
1) 一对一合并
df1 = DataFrame({'employee':['Bob','Jake','Lisa'],
'group':['Accounting','Engineering','Engineering'],
})
df1
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | |
|---|---|---|
| 0 | Bob | Accounting |
| 1 | Jake | Engineering |
| 2 | Lisa | Engineering |
df2 = DataFrame({'employee':['Lisa','Bob','Jake'],
'hire_date':[2004,2008,2012],
})
df2
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | hire_date | |
|---|---|---|
| 0 | Lisa | 2004 |
| 1 | Bob | 2008 |
| 2 | Jake | 2012 |
pd.merge(df1, df2) # 按照employee进行了合并
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | |
|---|---|---|---|
| 0 | Bob | Accounting | 2008 |
| 1 | Jake | Engineering | 2012 |
| 2 | Lisa | Engineering | 2004 |
2) 多对一合并
df3 = DataFrame({
'employee':['Lisa','Jake'],
'group':['Accounting','Engineering'],
'hire_date':[2004,2016]})
df3
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | |
|---|---|---|---|
| 0 | Lisa | Accounting | 2004 |
| 1 | Jake | Engineering | 2016 |
df4 = DataFrame({'group':['Accounting','Engineering','Engineering'],
'supervisor':['Carly','Guido','Steve']
})
df4
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| group | supervisor | |
|---|---|---|
| 0 | Accounting | Carly |
| 1 | Engineering | Guido |
| 2 | Engineering | Steve |
pd.merge(df3, df4)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | supervisor | |
|---|---|---|---|---|
| 0 | Lisa | Accounting | 2004 | Carly |
| 1 | Jake | Engineering | 2016 | Guido |
| 2 | Jake | Engineering | 2016 | Steve |
3) 多对多合并
df1 = DataFrame({'employee':['Bob','Jake','Lisa'],
'group':['Accounting','Engineering','Engineering']})
df1
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | |
|---|---|---|
| 0 | Bob | Accounting |
| 1 | Jake | Engineering |
| 2 | Lisa | Engineering |
df2 = DataFrame({'group':['Engineering','Engineering','HR'],
'supervisor':['Carly','Guido','Steve']
})
df2
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| group | supervisor | |
|---|---|---|
| 0 | Engineering | Carly |
| 1 | Engineering | Guido |
| 2 | HR | Steve |
pd.merge(df1,df2,how='right') # right表示右连接
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | supervisor | |
|---|---|---|---|
| 0 | Jake | Engineering | Carly |
| 1 | Lisa | Engineering | Carly |
| 2 | Jake | Engineering | Guido |
| 3 | Lisa | Engineering | Guido |
| 4 | NaN | HR | Steve |
4) key的规范化
- 当列冲突时,即有多个列名称相同时,需要使用on=来指定哪一个列作为key,配合suffixes指定冲突列名
df1 = DataFrame({'employee':['Jack',"Summer","Steve"],
'group':['Accounting','Finance','Marketing']})
df1
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | |
|---|---|---|
| 0 | Jack | Accounting |
| 1 | Summer | Finance |
| 2 | Steve | Marketing |
df2 = DataFrame({'employee':['Jack','Bob',"Jake"],
'hire_date':[2003,2009,2012],
'group':['Accounting','sell','ceo']})
df2
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | |
|---|---|---|---|
| 0 | Jack | Accounting | 2003 |
| 1 | Bob | sell | 2009 |
| 2 | Jake | ceo | 2012 |
pd.merge(df1,df2,on='employee') # 默认按照employee和group进行合并,可以指定列名
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group_x | group_y | hire_date | |
|---|---|---|---|---|
| 0 | Jack | Accounting | Accounting | 2003 |
- 当两张表没有可进行连接的列时,可使用left_on和right_on手动指定merge中左右两边的哪一列列作为连接的列
df1 = DataFrame({'employee':['Bobs','Linda','Bill'],
'group':['Accounting','Product','Marketing'],
'hire_date':[1998,2017,2018]})
df1
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | |
|---|---|---|---|
| 0 | Bobs | Accounting | 1998 |
| 1 | Linda | Product | 2017 |
| 2 | Bill | Marketing | 2018 |
df2 = DataFrame({'name':['Lisa','Bobs','Bill'],
'hire_dates':[1998,2016,2007]})
df2
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| hire_dates | name | |
|---|---|---|
| 0 | 1998 | Lisa |
| 1 | 2016 | Bobs |
| 2 | 2007 | Bill |
pd.merge(df1,df2,left_on='employee',right_on='name',how='outer')
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| employee | group | hire_date | hire_dates | name | |
|---|---|---|---|---|---|
| 0 | Bobs | Accounting | 1998.0 | 2016.0 | Bobs |
| 1 | Linda | Product | 2017.0 | NaN | NaN |
| 2 | Bill | Marketing | 2018.0 | 2007.0 | Bill |
| 3 | NaN | NaN | NaN | 1998.0 | Lisa |
5) 内合并与外合并:out取并集 inner取交集
- 内合并:只保留两者都有的key(默认模式)
df6 = DataFrame({'name':['Peter','Paul','Mary'],
'food':['fish','beans','bread']}
)
df6
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| food | name | |
|---|---|---|
| 0 | fish | Peter |
| 1 | beans | Paul |
| 2 | bread | Mary |
df7 = DataFrame({'name':['Mary','Joseph'],
'drink':['wine','beer']})
df7
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| drink | name | |
|---|---|---|
| 0 | wine | Mary |
| 1 | beer | Joseph |
pd.merge(df6, df7)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| food | name | drink | |
|---|---|---|---|
| 0 | bread | Mary | wine |
- 外合并 how='outer':补NaN
pd.merge(df6, df7, how='outer')
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| food | name | drink | |
|---|---|---|---|
| 0 | fish | Peter | NaN |
| 1 | beans | Paul | NaN |
| 2 | bread | Mary | wine |
| 3 | NaN | Joseph | beer |
Pandas的拼接操作的更多相关文章
- (四)pandas的拼接操作
pandas的拼接操作 #重点 pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join 0. 回顾numpy的级联 import num ...
- Pandas 拼接操作 数据处理
数据分析 生成器 迭代器 装饰器 (两层传参) 单例模式() ios七层 io多路 数据分析:是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律 pandas的拼接操作 p ...
- pandas的apply操作
pandas的apply操作类似于Scala的udf一样方便,假设存在如下dataframe: id_part pred pred_class v_id 0 d [0.722817, 0.650064 ...
- 深度学习实践-强化学习-bird游戏 1.np.stack(表示进行拼接操作) 2.cv2.resize(进行图像的压缩操作) 3.cv2.cvtColor(进行图片颜色的转换) 4.cv2.threshold(进行图片的二值化操作) 5.random.sample(样本的随机抽取)
1. np.stack((x_t, x_t, x_t, x_t), axis=2) 将图片进行串接的操作,使得图片的维度为[80, 80, 4] 参数说明: (x_t, x_t, x_t, x_t) ...
- Pandas的基础操作(一)——矩阵表的创建及其属性
Pandas的基础操作(一)——矩阵表的创建及其属性 (注:记得在文件开头导入import numpy as np以及import pandas as pd) import pandas as pd ...
- python数据结构:pandas(2)数据操作
一.Pandas的数据操作 0.DataFrame的数据结构 1.Series索引操作 (0)Series class Series(base.IndexOpsMixin, generic.NDFra ...
- JavaScript如何实现字符串拼接操作
实际应用中,目标字符串的生成可能需要多个数据的拼接. 由于应用频繁,几乎是所有编程语言都必须掌握的操作,当然每种语言具有各自特点. 本文将通过代码实例详细介绍一下JavaScript如何实现字符串拼接 ...
- 数据分析05 /pandas的高级操作
数据分析05 /pandas的高级操作 目录 数据分析05 /pandas的高级操作 1. 替换操作 2. 映射操作 3. 运算工具 4. 映射索引 / 更改之前索引 5. 排序实现的随机抽样/打乱表 ...
- pandas 写csv 操作
pandas 写csv 操作 def show_history(self): df = pd.DataFrame() df['Time'] = pd.Series(self.time_hist) df ...
随机推荐
- CA认证机制的简明解释
公钥机制面临的问题: 假冒身份发布公钥! 可以用CA来认证公钥的身份.CA有点像公安局,公钥就像身份证.公安局可以向任何合法用户颁发身份证以证明其合法身份.第三方只要识别身份证的真伪就能判断身份证持有 ...
- css定位选择兄弟元素,nth-of-type
<span class="input-group-btn" the-id="num-change"> <button class=" ...
- maven 提取jar包 依赖及打包排除
<properties> <project.targetDir>D:\jar</project.targetDir> <project.targetServe ...
- [php代码审计] 哈希长度拓展攻击
已知: 1. salt的长度. 2. message==“adminadmin”. 3. MD5(salt+message)的值. 求: MD5(salt+message+填充数据+任意字符串)的值. ...
- $_POST 和 php://input 的区别
手册中摘取的几句话: 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时, ...
- PCB项目 X1 STC12C5A60S2-LQPF48
单片机控制系统双层板STC51 简介: STC12C5A60S2主芯片,12MHz主频 12V电源输入,12/5/3V电源输出 4路0~12V可调10位ADC输入 4路1A大电流达林顿输出 4路INT ...
- 前端每日实战:35# 视频演示如何把 CSS 径向渐变用得出神入化,只用一个 DOM 元素就能画出国宝熊猫
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odKrpy 可交互视频教程 此视频 ...
- TreeMap和Comparable接口
备注:HashMap线程不安全,效率高,允许key.value为空 HasTable线程安全.效率低.不允许key或value为空 TreeMap在存储时会自动调用comparable方法进行排序,当 ...
- PCA(基础知识)
参考:http://blog.csdn.net/wangjian1204/article/details/50642732 参考:https://www.zhihu.com/question/3831 ...
- 对logistic回归分析的两重认识
logistic回归,回归给人的直观印象只是要求解一个模型的系数,然后可以预测某个变量的回归值.而logistic回归在应用中多了一层含义,它经常应用于分类中.第一重认识:logistic是给真正的回 ...