Pandas中Merge、Join与Concat合并数据的性能差异分析
本文深入对比Pandas中Merge、Join和Concat三种数据合并方法的执行效率。通过从100万至1000万行的实验数据发现,Join操作在处理大规模数据时耗时显著低于Merge,且随数据量增长线性增加的速度更慢。建议处理海量数据时优先使用Join以提升性能。
在Pandas库中,存在多种对DataFrame(数据框)进行合并处理的方式。
本文将深入探讨这些不同的合并方法,并对它们的执行速度进行对比分析。
数据合并操作
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
数据连接操作
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和Join操作均可针对指定列执行合并(类似于SQL中的join)。那么,它们的执行效率是否一致?下面我们将进行实测。
实验中,两个DataFrame包含相同的行数和两列,测试范围涵盖从100万行至1000万行的不同大小DataFrame,每次实验增加100万行。对固定行数的数据重复进行十次实验,以消除随机性影响。以下是这十次试验中合并操作的平均运行时间。

上图展示了各项操作所花费的时间(单位:毫秒)。
从图中可以看出,运行时间存在显著差异,最大相差可达5倍。随着DataFrame规模的增大,运行时间之间的差距也随之扩大。两种JOIN操作的耗时几乎都随DataFrame大小呈线性增加,但Join的运行时间增长速度远低于Merge。
因此,若需处理海量数据,建议优先使用join()进行操作。
至此,关于Pandas中Merge、Join与Concat合并数据效率对比的分析介绍完毕。如需了解更多Pandas合并数据效率相关内容,欢迎查阅本站历史文章或继续浏览后续文章,感谢大家的支持!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













