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

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

ztj100 2025-02-18 14:23 18 浏览 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()进行操作。

相关推荐

其实TensorFlow真的很水无非就这30篇熬夜练

好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...

交叉验证和超参数调整:如何优化你的机器学习模型

准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...

机器学习交叉验证全指南:原理、类型与实战技巧

机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...

深度学习中的类别激活热图可视化

作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...

超强,必会的机器学习评估指标

大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...

机器学习入门教程-第六课:监督学习与非监督学习

1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...

Python教程(三十八):机器学习基础

...

Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置

你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...

超全面讲透一个算法模型,高斯核!!

...

神经网络与传统统计方法的简单对比

传统的统计方法如...

AI 基础知识从0.1到0.2——用“房价预测”入门机器学习全流程

...

自回归滞后模型进行多变量时间序列预测

下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...

苹果AI策略:慢哲学——科技行业的“长期主义”试金石

苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...

时间序列预测全攻略,6大模型代码实操

如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...

AI 基础知识从 0.4 到 0.5—— 计算机视觉之光 CNN

...

取消回复欢迎 发表评论: