Pandas使用Merge与Join和Concat分别进行合并数据效率对比分析
作者:宋宋讲编程 发布时间:2023-03-13 12:14:01
在 Pandas 中有很多种方法可以进行dataframe(数据框)的合并。
本文将研究这些不同的方法,以及如何将它们执行速度的对比。
合并DF
Pandas 使用 .merge() 方法来执行合并。
import pandas as pd
# a dictionary to convert to a dataframe
data1 = {'identification': ['a', 'b', 'c', 'd'],
'Customer_Name':['King', 'West', 'Adams', 'Mercy'], 'Category':['furniture', 'Office Supplies', 'Technology', 'R_materials'],}
# our second dictionary to convert to a dataframe
data2 = {'identification': ['a', 'b', 'c', 'd'],
'Class':['First_Class', 'Second_Class', 'Same_day', 'Standard Class'],
'Age':[60, 30, 40, 50]}
# Convert the dictionary into DataFrame
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
运行我们的代码后,有两个 DataFrame,如下所示。
identification Customer_Name Category
0 a King furniture
1 b West Office Supplies
2 c Adams Technology
3 d Mercy R_materials
identification Class Age
0 a First_Class 60
1 b Second_Class 30
2 c Same_day 40
3 d Standard Class 50
使用 merge() 函数进一步合并。
import pandas as pd
df1=...
df2=...
x= pd. merge( df1,df2,
left_on = "df1_col1",
right_on = "df2_col1" )
# using .merge() function
new_data = pd.merge(df1, df2, on='identification')
这产生了下面的新数据;
identification Customer_Name Category Class Age
0 a King furniture First_Class 60
1 b West Office Supplies Second_Class 30
2 c Adams Technology Same_day 40
3 d Mercy R_materials Standard Class 50
.join() 方法也可以将不同索引的 DataFrame 组合成一个新的 DataFrame。我们可以使用参数‘on’参数指定根据哪列进行合并。
import pandas as pd
df1 = ...
df2 = ...
df1.set_index ( "df1_col1", inplace = True)
df2.set_index ( "df2_col1", inplace = True)
x=df1.join( df2)
让我们看看下面的例子,我们如何将单索引 DataFrame 与多索引 DataFrame 连接起来;
import pandas as pd
# a dictionary to convert to a dataframe
data1 = {
'Customer_Name':['King', 'West', 'Adams'],
'Category':['furniture', 'Office Supplies', 'Technology'],} 7
# our second dictionary to convert to a dataframe
data2 = {
'Class':['First_Class', 'Second_Class', 'Same_day', 'Standard Class'],
'Age':[60, 30, 40, 50]}
# Convert the dictionary into DataFrame
Ndata = pd.DataFrame(data1, index=pd.Index(['a', 'b', 'c'], name='identification'))
index = pd.MultiIndex.from_tuples([('a', 'x0'), ('b', 'x1'),
('c', 'x2'), ('c', 'x3')],
names=['identification', 'x']) 19
# Convert the dictionary into DataFrame
Ndata2 = pd.DataFrame(data2, index= index)
print(Ndata, "\n\n", Ndata2)
# joining singly indexed with
# multi indexed
result = Ndata.join(Ndata2, how='inner')
我们的结果如下所示;
Customer_Name Category Class Age
identification x 3 a x0 King furniture First_Class 60
b x1 West Office Supplies Second_Class 30
c x2 Adams Technology Same_day 40
x3 Adams Technology Standard Class 50
连接DF
Pandas 中concat() 方法在可以在垂直方向(axis=0)和水平方向(axis=1)上连接 DataFrame。我们还可以一次连接两个以上的 DataFrame 或 Series。
让我们看一个如何在 Pandas 中执行连接的示例;
import pandas as pd
# a dictionary to convert to a dataframe
data1 = {'identification': ['a', 'b', 'c', 'd'],
'Customer_Name':['King', 'West', 'Adams', 'Mercy'],
'Category':['furniture', 'Office Supplies', 'Technology', 'R_materials'],}
# our second dictionary to convert to a dataframe
data2 = {'identification': ['a', 'b', 'c', 'd'],
'Class':['First_Class', 'Second_Class', 'Same_day', 'Standard Class'],
'Age':[60, 30, 40, 50]}
# Convert the dictionary into DataFrame
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
#perform concatenation here based on horizontal axis
new_data = pd.concat([df1, df2], axis=1)
print(new_data)
这样就获得了新的 DataFrame :
identification Customer_Name Category identification \
0 a King furniture a 3 1 b West Office Supplies b 4 2 c Adams Technology c 5 3 d Mercy R_materials d
Class Age
0 First_Class 60
1 Second_Class 30
2 Same_day 40
3 Standard Class 50
Merge和Join的效率对比
Pandas 中的Merge Joins操作都可以针对指定的列进行合并操作(SQL中的join)那么他们的执行效率是否相同呢?下面我们来进行一下测。
两个 DataFrame 都有相同数量的行和两列,实验中考虑了从 100 万行到 1000 万行的不同大小的 DataFrame,并在每次实验中将行数增加了 100 万。我对固定数量的行重复了十次实验,以消除任何随机性。下面是这十次试验中合并操作的平均运行时间。
上图描绘了操作所花费的时间(以毫秒为单位)。
正如我们从图中看到的,运行时间存在显着差异——最多相差 5 倍。随着 DataFrame 大小的增加,运行时间之间的差异也会增加。两个 JOIN 操作几乎都随着 DataFrame 的大小线性增加。但是,Join的运行时间增加的速度远低于Merge。
如果需要处理大量数据,还是请使用join()进行操作。
来源:https://blog.csdn.net/qiqi1220/article/details/128219967
猜你喜欢
- 在asp中调用sql server的存储过程可以加快程序运行速度,本文介绍了asp使用存储过程的方法。1.调用存储过程的一般方法 先假设在s
- 要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清除的概念。大多IE下的显示错误,就
- 有时你会发现你写的视图函数是十分类似的,只有一点点的不同。 比如说,你有两个视图,它们的内容是一致的,除了它们所用的模板不太一样:# url
- 1.max取最大值函数max() 方法返回给定参数的最大值,参数可以为序列。lis = [1,2,3,-4]print(max(lis))
- 这篇文章主要介绍了如何获取Python简单for循环索引,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文实例讲述了php中数字、字符与对象判断函数用法。分享给大家供大家参考。具体分析如下:在php判断数字,字符,对象,数组等包括有参见 is
- Oracle客户端NLS_LANG设置 OracleWindowsMicrosoftUnixBash 1. NLS_LANG 参数组成 NL
- 将SQL Server中所有表的列信息显示出来: SELECT SysObjects.Name as tb_name, SysColumns
- 认为整理的还比较详细的,亲们,就快点收藏起来吧!PHP系统类函数assert函数:检查assertion声明是否错误extension_lo
- ASP开发网页牢记注意事项 选择自 RAINMAN_NET&
- 此前piscdong已经做过一次评测了,这次的Beta正式推出,我也来参与一下.当回小白鼠吧.新的界面设计非常让人兴奋750){this.r
- 概述从今天开始我们将开启一段自然语言处理 (NLP) 的旅程. 自然语言处理可以让来处理, 理解, 以及运用人类的语言, 实现机器语言和人类
- 1. 使用 fileinput 进行迭代fileinput 模块可以对一个或多个文件中的内容进行迭代、遍历等操作。该模块的 input()
- 本文实例讲述了Python反射和内置方法重写操作。分享给大家供大家参考,具体如下:isinstance和issubclassisinstan
- 最近开始学习Python,但只限于看理论,编几行代码,觉得没有意思,就想能不能用Python编写可视化的界面。遂查找了相关资料,发现了PyQ
- PHP Warning: strtotime(): It is not safe to rely on the system's t
- 一、前言上次写了一个俄罗斯方块,感觉好像大家都看懂了,这次就更新一个植物大战僵尸吧二、引入模块import pygameimport ran
- 从本文开始,本系列将介绍python简单案例并进行代码展示,本文的案例是利用pandas库实现读取csv文件并按照列的从小到大进行排序。前言
- Django1.8.2中文文档:Django1.8.2中文文档上传图片配置上传文件保存目录1)新建上传文件保存目录。2)配置上传文件保存目录
- 一个改进的仿google页面拖拽效果,移植方便。web2.0网站经常会用有这个拖拽页面布局的功能,如果你也想给你的网站加上这个有趣的功能,不