python merge、join、concat用法与区别
由于合并变化较大,以后函数可能会修改,只给出一些例子作为参考
总结:
merge、join
1、当没有索引时:merge、join为按照一定条件合并
2、当有索引、并按照索引合并时,得到结果为两者混合到一起了,重新按照一定规则排序了。
3、当没有索引时、concat不管列名,直接加到一起,可以加到后面、也可以加到右边,axis=0为加到后面,axis=1为加到右边,左边的数据结构没有变,变的是右边数据结构。
4、当有索引、并按照索引合并时,得到结果两者混合到一起了。
import pandas as pd
import numpy as np
from pandas import DataFrame,Series
data1=pd.DataFrame(np.arange(6).reshape(2,3),columns=list('abc'))
data2=pd.DataFrame(np.arange(20,26).reshape(2,3),columns=list('ayz')) data1
Out[33]:
a b c
0 0 1 2
1 3 4 5
data2
Out[34]:
a y z
0 20 21 22
1 23 24 25 data=pd.concat([data1,data2],axis=0)
data
Out[22]:
a b c y z
0 0 1.0 2.0 NaN NaN
1 3 4.0 5.0 NaN NaN
0 20 NaN NaN 21.0 22.0
1 23 NaN NaN 24.0 25.0
data=pd.concat([data1,data2],axis=1)
data
Out[24]:
a b c a y z
0 0 1 2 20 21 22
1 3 4 5 23 24 25
data=pd.merge(data1,data2)
data
Out[26]:
Empty DataFrame
Columns: [a, b, c, y, z] #a列没有共同元素
Index: []
data=pd.merge(data1,data2,on='a')
data
Out[28]:
Empty DataFrame
Columns: [a, b, c, y, z]
Index: []
data=pd.merge(data1,data2,on='a',how='outer')
data
Out[30]:
data
Out[30]:
a b c y z
0 0 1.0 2.0 NaN NaN
1 3 4.0 5.0 NaN NaN
2 20 NaN NaN 21.0 22.0
3 23 NaN NaN 24.0 25.0
data=pd.merge(data1,data2,how='outer')
data
Out[32]:
a b c y z
0 0 1.0 2.0 NaN NaN
1 3 4.0 5.0 NaN NaN
2 20 NaN NaN 21.0 22.0
3 23 NaN NaN 24.0 25.0
MJ数据处理:
方法一:reindex
A(少量数据)中数据按照B的数据重新排序,再将A中数据放入到B某一列中
这样不行,重排列后A中特有数据没有了
import pandas as pd
import numpy as np
from pandas import DataFrame,Series
data_a=pd.read_excel('A.xlsx',index_col=2).loc[:,'授信敞口额度'];print(data_a.head())
data_b=pd.read_excel('B.xlsx',index_col=0);print(data_b.head())
data_a=data_a.reindex(index=data_b.index)
data_b.iloc[:,9]=data_a
data_b.to_excel('new_data.xlsx')
方法二:concat
https://stackoverflow.com/questions/27719407/pandas-concat-valueerror-shape-of-passed-values-is-blah-indices-imply-blah2
print(data_a.index.is_unique,data_b.index.is_unique)
data=pd.concat([data_b,data_a],axis=1) #True False
#ValueError: Shape of passed values is (21, 378), indices imply (21, 288)
因为索引有重复项,所以不能concat
dataframe中去掉重复行
https://stackoverflow.com/questions/13035764/remove-rows-with-duplicate-indices-pandas-dataframe-and-timeseries/34297689#34297689
df3 = df3[~df3.index.duplicated(keep='first')]
#下面只能去掉同样的行,不能去掉索引相同行元素不同行
data_a.drop_duplicates(inplace=True);data_b.drop_duplicates(inplace=True)
data=pd.concat([data_b,data_a],axis=1)
还是不可以,由于有索引,结果会按照索引排序。
方法三:join
data=data_b.join(data_a,how='outer')
data.to_excel('data_join.xlsx')
也不可以,排序不是按照B中的数据在前,A中有B中没有数据在后,不太满足要求,较前面两种方法好
方法四:merge
data=pd.merge(data_b,data_a,left_index=True,right_index=True,how='outer')
data.to_excel('data_merge.xlsx')
与join方法得到结果一致,一样功能,不太满足需求。
方法五:merge
##合并数据不放在索引上,放在列上,没有索引,按列进行合并,结果直接在后面加,排列默认以合并左边列先排列,再排右边列。
#如果把公共列放在索引上,则返回结果会排序,merge、concat、join都会。
a = pd.read_excel('8月.xlsx')
b = pd.read_excel('8月末.xlsx')
print(b.head(),a.head())
c=pd.merge(a,b,on='客户名称',how='outer')
python merge、join、concat用法与区别的更多相关文章
- python merge、concat合并数据集
数据规整化:合并.清理.过滤 pandas和python标准库提供了一整套高级.灵活的.高效的核心函数和算法将数据规整化为你想要的形式! 本篇博客主要介绍: 合并数据集:.merge()..conca ...
- python pandas 合并数据函数merge join concat combine_first 区分
pandas对象中的数据可以通过一些内置的方法进行合并:pandas.merge,pandas.concat,实例方法join,combine_first,它们的使用对象和效果都是不同的,下面进行区分 ...
- merge,join,concat
merge交集 join并集 concat axis=0 竖着连 axis=1 横着连
- Python多线程join的用法
import threading, time def Myjoin(): print 'hello world!' time.sleep(1) for i in range(5): t=threadi ...
- python中join的用法
str.join(sequence) # 将序列中的元素以str字符连接生成一个新的字符串 list1 = ['a', 'b', 'c'] new_str = '-'.join(list1) # 输出 ...
- python中join函数用法
str.join(list/tuple/dict/string) str = "-"; seq = ("a", "b", "c&q ...
- python中一些相似用法的区别:index()和find(),dict[]和get()
index和find在字符串中的区别: index()方法和find()方法相似,唯一的区别就是find方法不包含索引值会返回-1,而index()不包含索引值会抛出异常 同样的:获取字典dict ...
- python ord()与chr()用法以及区别
ord()函数主要用来返回对应字符的ascii码,chr()主要用来表示ascii码对应的字符他的输入时数字,可以用十进制,也可以用十六进制. >>> ord("a&quo ...
- Python中threading的join和setDaemon的区别及用法[例子]
Python多线程编程时,经常会用到join()和setDaemon()方法,今天特地研究了一下两者的区别. 1.join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join() ...
随机推荐
- pexpect &&pxssh
python 3.6 pip install pexpect #!/usr/bin/python3 import os import sys curPath = os.path.abspath(os ...
- Oracle Parallel使用方法
一. 并行查询 并行查询允许将一个sql select语句划分为多个较小的查询,每个部分的查询并发地运行,然后将各个部分的结果组合起来,提供最终的结果,多用于全表扫描,索引全扫描等,大表的扫描和连接. ...
- 模块学习--OS
1 返回当前目录信息 >>> os.getcwd() 'D:\\7_Python\\S14' 2 改变路径 >>> os.chdir('d:\\')#os.chdi ...
- PTA的Python练习题(六)
从 第3章-8 字符串逆序 开始 1. n = str(input()) n1=n[::-1] print(n1) 2. 不是很好做这道题,自己还是C语言的思维,网上几乎也找不到什么答案 s = in ...
- Spark入门:第2节 Spark集群安装:1 - 3;第3节 Spark HA高可用部署:1 - 2
三. Spark集群安装 3.1 下载spark安装包 下载地址spark官网:http://spark.apache.org/downloads.html 这里我们使用 spark-2.1.3-bi ...
- Linux命令:top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- Codeforces1300C-Anu Has a Function
定义一个函数f(x,y), f(x,y) = x|y - y,给你一个数列,a1,a2,,an问如何排列能使f(f(f(a1,a2),a3),````,an)答案最大,我们将f(x,y)变形,就是f( ...
- SpringMVC 接收表单数据、数据绑定、解决请求参数中文乱码
接收表单数据有3种方式. 1.使用简单类型接收表单数据(绑定简单数据类型) 表单: <form action="${pageContext.request.contextPath}/u ...
- 网易云信-新增自定义消息(iOS版)
https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...
- 搭建springboot的ssm(spring + springmvc + mybatis)的maven项目
最终项目目录结构 创建过程 1.创建开关SpringBootApplication 为了创建快速.我们使用idea自带的创建springboot来创建结构,当然创建普通的web项目也是可以的.(使用e ...