91aaa在线国内观看,亚洲AV午夜福利精品一区二区,久久偷拍人视频,久久播这里有免费视播

<strong id="fvuar"></strong>

  • <sub id="fvuar"><dl id="fvuar"><em id="fvuar"></em></dl></sub>

    1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

      手機(jī)站
      千鋒教育

      千鋒學(xué)習(xí)站 | 隨時隨地免費學(xué)

      千鋒教育

      掃一掃進(jìn)入千鋒手機(jī)站

      領(lǐng)取全套視頻
      千鋒教育

      關(guān)注千鋒學(xué)習(xí)站小程序
      隨時隨地免費學(xué)習(xí)課程

      當(dāng)前位置:首頁  >  技術(shù)干貨  > Pandas中使用Merge、Join 、Concat合并數(shù)據(jù)效率對比

      Pandas中使用Merge、Join 、Concat合并數(shù)據(jù)效率對比

      來源:千鋒教育
      發(fā)布人:qyf
      時間: 2023-02-22 17:48:00 1677059280

        在 Pandas 中有很多種方法可以進(jìn)行dataframe(數(shù)據(jù)框)的合并。

        本文將研究這些不同的方法,以及如何將它們執(zhí)行速度的對比。

        合并DF

      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() 函數(shù)進(jìn)一步合并。

      圖片 1

      # using .merge() function  
      new_data = pd.merge(df1, df2, on='identification')

        這產(chǎn)生了下面的新數(shù)據(jù);

      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。我們可以使用參數(shù)‘on’參數(shù)指定根據(jù)哪列進(jìn)行合并。

      圖片 2

        讓我們看看下面的例子,我們?nèi)绾螌嗡饕?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')

        我們的結(jié)果如下所示;

      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 中執(zhí)行連接的示例;

      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操作都可以針對指定的列進(jìn)行合并操作(SQL中的join)那么他們的執(zhí)行效率是否相同呢?下面我們來進(jìn)行一下測。

        兩個 DataFrame 都有相同數(shù)量的行和兩列,實驗中考慮了從 100 萬行到 1000 萬行的不同大小的 DataFrame,并在每次實驗中將行數(shù)增加了 100 萬。我對固定數(shù)量的行重復(fù)了十次實驗,以消除任何隨機(jī)性。下面是這十次試驗中合并操作的平均運行時間。

      圖片 3

        上圖描繪了操作所花費的時間(以毫秒為單位)。

        正如我們從圖中看到的,運行時間存在顯著差異——最多相差 5 倍。隨著 DataFrame 大小的增加,運行時間之間的差異也會增加。兩個 JOIN 操作幾乎都隨著 DataFrame 的大小線性增加。但是,Join的運行時間增加的速度遠(yuǎn)低于Merge。

        如果需要處理大量數(shù)據(jù),還是請使用join()進(jìn)行操作。

      tags:
      聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
      10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
      請您保持通訊暢通,專屬學(xué)習(xí)老師24小時內(nèi)將與您1V1溝通
      免費領(lǐng)取
      今日已有369人領(lǐng)取成功
      劉同學(xué) 138****2860 剛剛成功領(lǐng)取
      王同學(xué) 131****2015 剛剛成功領(lǐng)取
      張同學(xué) 133****4652 剛剛成功領(lǐng)取
      李同學(xué) 135****8607 剛剛成功領(lǐng)取
      楊同學(xué) 132****5667 剛剛成功領(lǐng)取
      岳同學(xué) 134****6652 剛剛成功領(lǐng)取
      梁同學(xué) 157****2950 剛剛成功領(lǐng)取
      劉同學(xué) 189****1015 剛剛成功領(lǐng)取
      張同學(xué) 155****4678 剛剛成功領(lǐng)取
      鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
      董同學(xué) 138****2867 剛剛成功領(lǐng)取
      周同學(xué) 136****3602 剛剛成功領(lǐng)取
      相關(guān)推薦HOT
      python字符串截???

      在Python中,字符串是一種非常常見的數(shù)據(jù)類型,它可以用來表示文本、數(shù)字、符號等內(nèi)容。在實際應(yīng)用中,我們經(jīng)常需要對字符串進(jìn)行截取,以便獲取...詳情>>

      2023-11-02 17:56:27
      Python socket C/S結(jié)構(gòu)的聊天室應(yīng)用實現(xiàn)?

      隨著互聯(lián)網(wǎng)的發(fā)展,聊天室應(yīng)用成為人們?nèi)粘I钪惺殖R姷囊环N社交方式。Python語言的Socket模塊是實現(xiàn)網(wǎng)絡(luò)通信的重要工具,可以輕松地實現(xiàn)C/...詳情>>

      2023-11-02 17:53:38
      用while求1到100的奇數(shù)和?

      在計算機(jī)編程中,循環(huán)語句是非常重要的一部分。而while語句是其中最基本也是最常用的一種。它的作用是在滿足一定條件的情況下,重復(fù)執(zhí)行一段代...詳情>>

      2023-11-02 17:50:57
      python創(chuàng)建一個集合?

      在Python中,集合是一種無序且不重復(fù)的數(shù)據(jù)類型,可以用于存儲一組元素。創(chuàng)建一個集合非常簡單,只需要使用大括號{}或者set()函數(shù)即可。使用大...詳情>>

      2023-11-02 17:34:02
      linux改文件屬主命令?

      Linux文件相關(guān)命令1、命令一:cat cat命令應(yīng)該是在Linux中查看文件內(nèi)容最常見的命令了。使用cat命令會打印指定文件的所有內(nèi)容到標(biāo)準(zhǔn)輸出上,比...詳情>>

      2023-10-31 19:58:15