網(wǎng)站首頁 編程語言 正文
merge()
1.常規(guī)合并
①方法1
指定一個(gè)參照列,以該列為準(zhǔn),合并其他列。
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '003'],
'num1': [120, 101, 104],
'num2': [110, 102, 121],
'num3': [105, 120, 113]})
df2 = pd.DataFrame({'id': ['001', '002', '003'],
'num4': [80, 86, 79]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
df_merge = pd.merge(df1, df2, on='id')
print(df_merge)
②方法2
要實(shí)現(xiàn)該合并,也可以通過索引來合并,即以index列為基準(zhǔn)。將left_index 和 right_index 都設(shè)置為True
即可。(left_index 和 right_index 都默認(rèn)為False,left_index表示左表以左表數(shù)據(jù)的index為基準(zhǔn), right_index表示右表以右表數(shù)據(jù)的index為基準(zhǔn)。)
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '003'],
'num1': [120, 101, 104],
'num2': [110, 102, 121],
'num3': [105, 120, 113]})
df2 = pd.DataFrame({'id': ['001', '002', '003'],
'num4': [80, 86, 79]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
df_merge = pd.merge(df1, df2, left_index=True, right_index=True)
print(df_merge)
相比方法①,區(qū)別在于,如圖,方法②合并出的數(shù)據(jù)中有重復(fù)列。
重要參數(shù)
pd.merge(right,how=‘inner’, on=“None”, left_on=“None”, right_on=“None”, left_index=False, right_index=False )
參數(shù) | 描述 |
---|---|
left | 左表,合并對(duì)象,DataFrame或Series |
right | 右表,合并對(duì)象,DataFrame或Series |
how | 合并方式,可以是left(左合并), right(右合并), outer(外合并), inner(內(nèi)合并) |
on | 基準(zhǔn)列 的列名 |
left_on | 左表基準(zhǔn)列列名 |
right_on | 右表基準(zhǔn)列列名 |
left_index | 左列是否以index為基準(zhǔn),默認(rèn)False,否 |
right_index | 右列是否以index為基準(zhǔn),默認(rèn)False,否 |
其中,left_index與right_index 不能與 on 同時(shí)指定。
合并方式 left right outer inner
準(zhǔn)備數(shù)據(jù)‘
新準(zhǔn)備一組數(shù)據(jù):
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '003'],
'num1': [120, 101, 104],
'num2': [110, 102, 121],
'num3': [105, 120, 113]})
df2 = pd.DataFrame({'id': ['001', '004', '003'],
'num4': [80, 86, 79]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
inner(默認(rèn))
使用來自兩個(gè)數(shù)據(jù)集的鍵的交集
df_merge = pd.merge(df1, df2, on='id')
print(df_merge)
outer
使用來自兩個(gè)數(shù)據(jù)集的鍵的并集
df_merge = pd.merge(df1, df2, on='id', how="outer")
print(df_merge)
left
使用來自左數(shù)據(jù)集的鍵
df_merge = pd.merge(df1, df2, on='id', how='left')
print(df_merge)
right
使用來自右數(shù)據(jù)集的鍵
df_merge = pd.merge(df1, df2, on='id', how='right')
print(df_merge)
2.多對(duì)一合并
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '003'],
'num1': [120, 101, 104],
'num2': [110, 102, 121],
'num3': [105, 120, 113]})
df2 = pd.DataFrame({'id': ['001', '001', '003'],
'num4': [80, 86, 79]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
如圖,df2中有重復(fù)id1的數(shù)據(jù)。
合并
df_merge = pd.merge(df1, df2, on='id')
print(df_merge)
合并結(jié)果如圖所示:
依然按照默認(rèn)的Inner方式,使用來自兩個(gè)數(shù)據(jù)集的鍵的交集。且重復(fù)的鍵的行會(huì)在合并結(jié)果中體現(xiàn)為多行。
3.多對(duì)多合并
如圖表1和表2中都存在多行id重復(fù)的。
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '002', '002', '003'],
'num1': [120, 101, 104, 114, 123],
'num2': [110, 102, 121, 113, 126],
'num3': [105, 120, 113, 124, 128]})
df2 = pd.DataFrame({'id': ['001', '001', '002', '003', '001'],
'num4': [80, 86, 79, 88, 93]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
df_merge = pd.merge(df1, df2, on='id')
print(df_merge)
concat()
pd.concat(objs, axis=0, join=‘outer’, ignore_index:bool=False,keys=None,levels=None,names=None, verify_integrity:bool=False,sort:bool=False,copy:bool=True)
參數(shù) | 描述 |
---|---|
objs | Series,DataFrame或Panel對(duì)象的序列或映射 |
axis | 默認(rèn)為0,表示列。如果為1則表示行。 |
join | 默認(rèn)為"outer",也可以為"inner" |
ignore_index | 默認(rèn)為False,表示保留索引(不忽略)。設(shè)為True則表示忽略索引。 |
其他重要參數(shù)通過實(shí)例說明。
1.相同字段的表首位相連
首先準(zhǔn)備三組DataFrame數(shù)據(jù):
import pandas as pd
df1 = pd.DataFrame({'id': ['001', '002', '003'],
'num1': [120, 114, 123],
'num2': [110, 102, 121],
'num3': [113, 124, 128]})
df2 = pd.DataFrame({'id': ['004', '005'],
'num1': [120, 101],
'num2': [113, 126],
'num3': [105, 128]})
df3 = pd.DataFrame({'id': ['007', '008', '009'],
'num1': [120, 101, 125],
'num2': [113, 126, 163],
'num3': [105, 128, 114]})
print(df1)
print("=======================================")
print(df2)
print("=======================================")
print(df3)
合并
dfs = [df1, df2, df3]
result = pd.concat(dfs)
print(result)
如果想要在合并后,標(biāo)記一下數(shù)據(jù)都來自于哪張表或者數(shù)據(jù)的某類別,則也可以給concat加上 參數(shù)keys 。
result = pd.concat(dfs, keys=['table1', 'table2', 'table3'])
print(result)
此時(shí),添加的keys與原來的index組成元組,共同成為新的index。
print(result.index)
2.橫向表合并(行對(duì)齊)
準(zhǔn)備兩組DataFrame數(shù)據(jù):
import pandas as pd
df1 = pd.DataFrame({'num1': [120, 114, 123],
'num2': [110, 102, 121],
'num3': [113, 124, 128]}, index=['001', '002', '003'])
df2 = pd.DataFrame({'num3': [117, 120, 101, 126],
'num5': [113, 125, 126, 133],
'num6': [105, 130, 128, 128]}, index=['002', '003', '004', '005'])
print(df1)
print("=======================================")
print(df2)
當(dāng)axis為默認(rèn)值0時(shí):
result = pd.concat([df1, df2])
print(result)
橫向合并需要將axis設(shè)置為1 :
result = pd.concat([df1, df2], axis=1)
print(result)
對(duì)比以上輸出差異。
- axis=0時(shí),即默認(rèn)縱向合并時(shí),如果出現(xiàn)重復(fù)的行,則會(huì)同時(shí)體現(xiàn)在結(jié)果中
- axis=1時(shí),即橫向合并時(shí),如果出現(xiàn)重復(fù)的列,則會(huì)同時(shí)體現(xiàn)在結(jié)果中。
3.交叉合并
result = pd.concat([df1, df2], axis=1, join='inner')
print(result)
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_48964486/article/details/123387448
相關(guān)推薦
- 2022-04-28 postman測(cè)試接口各種類型傳值的實(shí)現(xiàn)_相關(guān)技巧
- 2022-12-21 淺析Go語言的數(shù)據(jù)類型及數(shù)組_Golang
- 2022-08-05 C語言示例講解do?while循環(huán)語句的用法_C 語言
- 2022-11-13 Python?fileinput模塊應(yīng)用詳解_python
- 2022-09-06 詳解SQL?Server?中的?ACID?屬性_MsSql
- 2022-07-02 python之NAN和INF值處理方式_python
- 2022-01-18 報(bào)錯(cuò):Error occurred when invoke the listener‘s inter
- 2022-05-24 ASP.NET?MVC使用異步TPL模式_實(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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支