網站首頁 編程語言 正文
python DataFrame的合并方法
Python的Pandas針對DataFrame,Series提供了多個合并函數,通過參數的調整可以輕松實現DatafFrame的合并。
首先,定義3個DataFrame df1,df2,df3,進行concat、merge、append函數的實驗。
df1=pd.DataFrame([[1,2,3],[2,3,4]],columns=['a','b','c'])
df2=pd.DataFrame([[2,3,4],[3,4,5]],columns=['a','b','c'])
df3=pd.DataFrame([[1,2,3],[2,3,4]],columns=['a','b','d'])
df1
a b c
0 1 2 3
1 2 3 4
df2
a b c
0 2 3 4
1 3 4 5
df3
a b d
0 1 2 3
1 2 3 4
#concat函數
pandas中concat函數的完整表達,包含多個參數,常用的有axis,join,ignore_index.
concat函數的第一個參數為objs,一般為一個list列表,包含要合并兩個或多個DataFrame,多個Series
pandas.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
? ? ? ? ? ?keys=None, levels=None, names=None, verify_integrity=False,
? ? ? ? ? ?copy=True)
1.axis表示合并方向,默認axis=0,兩個DataFrame按照索引方向縱向合并,axis=1則會按照columns橫向合并。
pd.concat([df1,df2],axis=1)
a b c a b c
0 1 2 3 2 3 4
1 2 3 4 3 4 5
2.join表示合并方式,默認join=‘outer’,另外的取值為’inner’,只合并相同的部分,axis=0時合并結果為相同列名的數據,axis=1時為具有相同索引的數據
pd.concat([df2,df3],axis=0,join='inner')
a b
0 2 3
1 3 4
0 1 2
1 2 3
pd.concat([df2,df3],axis=1,join='inner')
a b c a b d
0 2 3 4 1 2 3
1 3 4 5 2 3 4
3.ignore_index表示索引的合并方式,默認為False,會保留原df的索引,如果設置ignore_index=True,合并后的df會重置索引。
pd.concat([df1,df2],ignore_index=True)
a b c
0 1 2 3
1 2 3 4
2 2 3 4
3 3 4 5
#merge函數
merge函數是pandas提供的一種數據庫式的合并方法。
on可以指定合并的列、索引,how則是與數據庫join函數相似,取值為left,right,outer,inner.left,right分別對應left outer join, right outer join.
pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
? ? ? ? ? left_index=False, right_index=False, sort=False,
? ? ? ? ? suffixes=('_x', '_y'), copy=True, indicator=False,
? ? ? ? ? validate=None):
merge函數可以通過pandas.merge(df1,df2)、df1.merge(df2)兩種形式來實現兩個DataFrame的合并,df1.merge(df2)是默認left=self的情況。
df_merge =df1.merge(df3,on=['a','b'])
? ?a ?b ?c ?d
0 ?1 ?2 ?3 ?3
1 ?2 ?3 ?4 ?4
#append函數
append函數是pandas針對DataFrame、Series等數據結構合并提供的函數。
df1.append(self, other, ignore_index=False, verify_integrity=False)
df1.append(df2)與pd.concat([df1,df2],ignore_index=False)具有相同的合并結果
df1.append(df2)
? ?a ?b ?c
0 ?1 ?2 ?3
1 ?2 ?3 ?4
0 ?2 ?3 ?4
1 ?3 ?4 ?5
更多使用方法可以參考pandas關于數據合并的官方文檔http://pandas.pydata.org/pandas-docs/stable/merging.html
把兩個dataframe合并成一個
1.merage
result = pd.merge(對象1, 對象2, on='key')
對象1 和 對象2分別為要合并的dataframe,key是在兩個dataframe都存在的列(類似于數據庫表中的主鍵)
2.append
result = df1.append(df2)
result = df1.append([df2, df3])
result = df1.append(df4, ignore_index=True)
3.join
result = left.join(right, on=['key1', 'key2'], how='inner')
4.concat
pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
? ? ?keys=None, levels=None, names=None, verify_integrity=False,
? ? ?copy=True)
frames = [df1, df2, df3]
result = pd.concat(frames)
result = pd.concat(frames, keys=['x', 'y', 'z'])
result = pd.concat([df1, df4], ignore_index=True)
原文鏈接:https://blog.csdn.net/haha_point/article/details/86512316
相關推薦
- 2022-11-24 Python文件簡單操作及openpyxl操作excel文件詳解_python
- 2024-02-26 Yaml數據讀取
- 2022-02-17 ERROR: but there is no HDFS_NAMENODE_USER defined.
- 2022-12-29 Python利用卡方Chi特征檢驗實現提取關鍵文本特征_python
- 2023-01-02 C++?命名空間?using聲明使用示例詳解_C 語言
- 2023-01-09 No?module?named?'plotly.graph_objects'報錯解決_python
- 2022-08-31 C語言數據結構之單鏈表與雙鏈表的增刪改查操作實現_C 語言
- 2022-03-06 css3溢出隱藏的方法_基礎教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支