最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pandas使用Merge與Join和Concat分別進(jìn)行合并數(shù)據(jù)效率對比分析

 更新時間:2022年12月08日 09:00:21   作者:宋宋講編程  
這篇文章主要給大家介紹了關(guān)于pandas中DataFrame數(shù)據(jù)合并連接(merge、join、concat)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下

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

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

合并DF

Pandas 使用 .merge() 方法來執(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)  

運行我們的代碼后,有兩個 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)一步合并。

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')

這產(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)行合并。

import pandas as pd
df1 = ...
df2 = ...
df1.set_index ( "df1_col1", inplace = True)
df2.set_index ( "df2_col1", inplace = True)
x=df1.join( df2)

讓我們看看下面的例子,我們?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ī)性。下面是這十次試驗中合并操作的平均運行時間。

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

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

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

到此這篇關(guān)于Pandas使用Merge與Join和Concat分別進(jìn)行合并數(shù)據(jù)效率對比分析的文章就介紹到這了,更多相關(guān)Pandas合并數(shù)據(jù)效率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中np.random.permutation函數(shù)實例詳解

    python中np.random.permutation函數(shù)實例詳解

    np.random.permutation是numpy中的一個函數(shù),它可以將一個數(shù)組中的元素隨機(jī)打亂,返回一個打亂后的新數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python中np.random.permutation函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • python如何計算圓的周長和面積

    python如何計算圓的周長和面積

    這篇文章主要介紹了python如何計算圓的周長和面積問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • GPU排隊腳本實現(xiàn)空閑觸發(fā)python腳本實現(xiàn)示例

    GPU排隊腳本實現(xiàn)空閑觸發(fā)python腳本實現(xiàn)示例

    有的服務(wù)器是多用戶使用,GPU的資源常常被占據(jù)著,很可能在夜間GPU空閑了,但來不及運行自己的腳本。如果沒有和別人共享服務(wù)器的話,自己的多個程序想排隊使用GPU,也可以用這個腳本
    2021-11-11
  • Python3.6 之后字典是有序的?

    Python3.6 之后字典是有序的?

    字典數(shù)據(jù)是有序的, 但是這個序不是由外部控制, 而是內(nèi)部字典定位機(jī)制的序 所以對外來講, 數(shù)據(jù)本身是無序的 你每次遍歷的順序一樣, 是因為枚舉結(jié)果是按內(nèi)部排序輸出 而無序則表示在你無法從外部控制最終的輸出順序,下面我們來學(xué)習(xí)Python字典有序性的相關(guān)資料又當(dāng)怎樣吧
    2021-12-12
  • python的去重以及數(shù)據(jù)合并的用法說明

    python的去重以及數(shù)據(jù)合并的用法說明

    這篇文章主要介紹了python的去重以及數(shù)據(jù)合并的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳解如何使用Pandas創(chuàng)建有效且可復(fù)制的代碼

    詳解如何使用Pandas創(chuàng)建有效且可復(fù)制的代碼

    Pandas作為一種多功能和強大的工具而屹立不倒,其直觀的數(shù)據(jù)結(jié)構(gòu)和廣泛的功能使其成為無數(shù)數(shù)據(jù)專業(yè)人士和愛好者的首選,本文將使用Pandas創(chuàng)建有效且可復(fù)制的代碼,感興趣的可以了解下
    2024-11-11
  • 四行Python3代碼實現(xiàn)圖片添加美顏效果

    四行Python3代碼實現(xiàn)圖片添加美顏效果

    這篇文章主要為大家介紹了如何利用Python語言實現(xiàn)給圖片添加美顏效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-04-04
  • 如何使用Selenium實現(xiàn)簡單的網(wǎng)絡(luò)自動化操作指南

    如何使用Selenium實現(xiàn)簡單的網(wǎng)絡(luò)自動化操作指南

    Selenium是一個用于Web應(yīng)用測試的工具,Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣,這篇文章主要給大家介紹了關(guān)于如何使用Selenium實現(xiàn)簡單的網(wǎng)絡(luò)自動化操作的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Python PEP8代碼規(guī)范常見問題以及解決方案

    Python PEP8代碼規(guī)范常見問題以及解決方案

    PEP8是Python的代碼規(guī)范,本文總結(jié)了常見的PEP8代碼規(guī)范問題及解決方法,幫助開發(fā)者編寫規(guī)范的代碼
    2024-11-11
  • Python 數(shù)據(jù)結(jié)構(gòu)之十大經(jīng)典排序算法一文通關(guān)

    Python 數(shù)據(jù)結(jié)構(gòu)之十大經(jīng)典排序算法一文通關(guān)

    排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存
    2021-10-10

最新評論

文安县| 克山县| 延川县| 会理县| 大余县| 当雄县| 墨竹工卡县| 临武县| 松潘县| 申扎县| 区。| 安塞县| 嘉峪关市| 清苑县| 修文县| 鹿邑县| 会理县| 金山区| 广河县| 耿马| 元阳县| 水富县| 拜泉县| 保定市| 雅江县| 沙坪坝区| 扶余县| 金昌市| 什邡市| 廊坊市| 陆良县| 麟游县| 嫩江县| 元朗区| 乡城县| 绿春县| 梨树县| 玉龙| 读书| 闽清县| 九龙坡区|