網(wǎng)站首頁 編程語言 正文
Pandas使用Merge與Join和Concat分別進(jìn)行合并數(shù)據(jù)效率對(duì)比分析_python
作者:宋宋講編程 ? 更新時(shí)間: 2023-01-05 編程語言在 Pandas 中有很多種方法可以進(jìn)行dataframe(數(shù)據(jù)框)的合并。
本文將研究這些不同的方法,以及如何將它們執(zhí)行速度的對(duì)比。
合并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)
運(yùn)行我們的代碼后,有兩個(gè) 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 組合成一個(gè)新的 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。我們還可以一次連接兩個(gè)以上的 DataFrame 或 Series。
讓我們看一個(gè)如何在 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的效率對(duì)比
Pandas 中的Merge Joins操作都可以針對(duì)指定的列進(jìn)行合并操作(SQL中的join)那么他們的執(zhí)行效率是否相同呢?下面我們來進(jìn)行一下測。
兩個(gè) DataFrame 都有相同數(shù)量的行和兩列,實(shí)驗(yàn)中考慮了從 100 萬行到 1000 萬行的不同大小的 DataFrame,并在每次實(shí)驗(yàn)中將行數(shù)增加了 100 萬。我對(duì)固定數(shù)量的行重復(fù)了十次實(shí)驗(yàn),以消除任何隨機(jī)性。下面是這十次試驗(yàn)中合并操作的平均運(yùn)行時(shí)間。
上圖描繪了操作所花費(fèi)的時(shí)間(以毫秒為單位)。
正如我們從圖中看到的,運(yùn)行時(shí)間存在顯著差異——最多相差 5 倍。隨著 DataFrame 大小的增加,運(yùn)行時(shí)間之間的差異也會(huì)增加。兩個(gè) JOIN 操作幾乎都隨著 DataFrame 的大小線性增加。但是,Join的運(yùn)行時(shí)間增加的速度遠(yuǎn)低于Merge。
如果需要處理大量數(shù)據(jù),還是請使用join()進(jìn)行操作。
原文鏈接:https://blog.csdn.net/qiqi1220/article/details/128219967
相關(guān)推薦
- 2023-11-15 Linux系統(tǒng)SSH客戶端斷開后保持進(jìn)程繼續(xù)運(yùn)行配置方法;Python等腳本在終端后臺(tái)運(yùn)行的方法
- 2022-09-08 Pandas中DataFrame的基本操作之重新索引講解_python
- 2022-09-08 Go語言中的Struct結(jié)構(gòu)體_Golang
- 2023-11-25 優(yōu)化計(jì)算屬性mapState、mapGetters和methods的mapActions、mapMu
- 2023-01-17 關(guān)于最大池化層和平均池化層圖解_python
- 2022-05-06 nginx?負(fù)載均衡輪詢方式配置詳解_nginx
- 2022-10-15 flask路由分模塊管理及自定義restful響應(yīng)格式詳解_python
- 2022-05-08 Entity?Framework根據(jù)實(shí)體的EntityState狀態(tài)實(shí)現(xiàn)增刪改查_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支