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

在 pandas 中有很多种方法可以进行df的合并。
本文将研究这些不同的方法,以及如何将它们执行速度的对比。
合并
pandas 使用 .merge() 方法来执行合并。
import pandas as pd   # a dictionary to convert to a dataframedata1 = {'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         category0             a         king       furniture1             b         west office supplies2             c         adams       technology3             d         mercy     r_materials  identification           class age0             a     first_class   601             b   second_class   302             c       same_day   403             d standard class   50  
使用 merge() 函数进一步合并。
# using .merge() function  new_data = pd.merge(df1, df2, on='identification')这产生了下面的新数据;identification customer_name category     class           age0     a           king         furniture     first_class     601     b           west         office supplies second_class   302     c           adams         technology     same_day     403     d           mercy         r_materials standard class   50  
.join() 方法也可以将不同索引的 dataframe 组合成一个新的 dataframe。我们可以使用参数‘on’参数指定根据哪列进行合并。
    让我们看看下面的例子,我们如何将单索引 dataframe 与多索引 dataframe 连接起来;
import pandas as pd  # a dictionary to convert to a dataframedata1 = {      '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, , ndata2)# joining singly indexed with# multi indexedresult = ndata.join(ndata2, how='inner')  
我们的结果如下所示;
customer_name       category     class       ageidentification x                                                     3 a         x0       king       furniture     first_class     60b         x1       west     office supplies   second_class   30c         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 dataframedata1 = {'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 axisnew_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()进行操作。


嵌入式工程师必备技能_关于嵌入式系统中的光电隔离设计技巧
IIC总线的主要特点/通信过程/读写过程
三星Galaxy S10 5G版将在明年2月发布该机有1TB机身存储
点亮户外世界的高品质连接器  Amass LC系列你值得拥有
十铨科技推出PCIe 4.0 SSD,采用石墨烯铜箔组合导热材质
Pandas中使用Merge、Join、Concat合并数据的效率对比
三星:全球半导体需求有望增长 存储芯片产业或趋于稳定
断开PCB表面走线的方法
河北移动已经在雄安新区完成重点区域的5G全面覆盖
免费GPU哪家强?谷歌Kaggle vs和Colab
70年代,人工智能进入了第一次低谷期
你懂得挑选智能门锁吗
中国芯呼声越来越高,国内EDA龙头华大九天新突破
杉杉股份再度跨界,正式进军LCD偏光片领域
【新品发布】Moku:Go 仪器套件新增数字滤波器、FIR滤波器生成器、锁相放大器功能
为您的物联网系统开发制定正确的路线
国内模拟IC企业该如何实现更大技术突破
联想Z6 Pro 5G开始预售 是迄今为止性价比最高的骁龙855 5G手机
智能手表的充电方法
中/美/阿火星探测器将在2月齐聚火星