百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

在 Pandas 中使用 Merge、Join 、Concat合并数据的效率对比

ztj100 2025-02-18 14:23 14 浏览 0 评论

在 Pandas 中有很多种方法可以进行DF的合并。本文将研究这些不同的方法,以及如何将它们应用到我们的数据中。

合并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() 函数进一步合并。

# 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’参数指定根据哪列进行合并。

让我们看看下面的例子,我们如何将单索引 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()进行操作。

相关推荐

Vue3非兼容变更——函数式组件(vue 兼容)

在Vue2.X中,函数式组件有两个主要应用场景:作为性能优化,因为它们的初始化速度比有状态组件快得多;返回多个根节点。然而在Vue3.X中,有状态组件的性能已经提高到可以忽略不计的程度。此外,有状态组...

利用vue.js进行组件化开发,一学就会(一)

组件原理/组成组件(Component)扩展HTML元素,封装可重用的代码,核心目标是为了可重用性高,减少重复性的开发。组件预先定义好行为的ViewModel类。代码按照template\styl...

Vue3 新趋势:10 个最强 X 操作!(vue.3)

Vue3为前端开发带来了诸多革新,它不仅提升了性能,还提供了...

总结 Vue3 组件管理 12 种高级写法,灵活使用才能提高效率

SFC单文件组件顾名思义,就是一个.vue文件只写一个组件...

前端流行框架Vue3教程:17. _组件数据传递

_组件数据传递我们之前讲解过了组件之间的数据传递,...

前端流行框架Vue3教程:14. 组件传递Props效验

组件传递Props效验Vue组件可以更细致地声明对传入的props的校验要求...

前端流行框架Vue3教程:25. 组件保持存活

25.组件保持存活当使用...

5 个被低估的 Vue3 实战技巧,让你的项目性能提升 300%?

前端圈最近都在卷性能优化和工程化,你还在用老一套的Vue3开发方法?作为摸爬滚打多年的老前端,今天就把私藏的几个Vue3实战技巧分享出来,帮你在开发效率、代码质量和项目性能上实现弯道超车!一、...

绝望!Vue3 组件频繁崩溃?7 个硬核技巧让性能暴涨 400%!

前端的兄弟姐妹们五一假期快乐,谁还没在Vue3项目上栽过跟头?满心欢喜写好的组件,一到实际场景就频频崩溃,页面加载慢得像蜗牛,操作卡顿到让人想砸电脑。用户疯狂吐槽,领导脸色难看,自己改代码改到怀疑...

前端流行框架Vue3教程:15. 组件事件

组件事件在组件的模板表达式中,可以直接使用...

Vue3,看这篇就够了(vue3 从入门到实战)

一、前言最近很多技术网站,讨论的最多的无非就是Vue3了,大多数都是CompositionAPI和基于Proxy的原理分析。但是今天想着跟大家聊聊,Vue3对于一个低代码平台的前端更深层次意味着什么...

前端流行框架Vue3教程:24.动态组件

24.动态组件有些场景会需要在两个组件间来回切换,比如Tab界面...

前端流行框架Vue3教程:12. 组件的注册方式

组件的注册方式一个Vue组件在使用前需要先被“注册”,这样Vue才能在渲染模板时找到其对应的实现。组件注册有两种方式:全局注册和局部注册...

焦虑!Vue3 组件频繁假死?6 个奇招让页面流畅度狂飙 500%!

前端圈的朋友们,谁还没在Vue3项目上踩过性能的坑?满心期待开发出的组件,一到高并发场景就频繁假死,用户反馈页面点不动,产品经理追着问进度,自己调试到心态炸裂!别以为这是个例,不少人在电商大促、数...

前端流行框架Vue3教程:26. 异步组件

根据上节课的代码,我们在切换到B组件的时候,发现并没有网络请求:异步组件:...

取消回复欢迎 发表评论: