關于python?DataFrame的合并方法總結
python DataFrame的合并方法
Python的Pandas針對DataFrame,Series提供了多個合并函數(shù),通過參數(shù)的調(diào)整可以輕松實現(xiàn)DatafFrame的合并。
首先,定義3個DataFrame df1,df2,df3,進行concat、merge、append函數(shù)的實驗。
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函數(shù)
pandas中concat函數(shù)的完整表達,包含多個參數(shù),常用的有axis,join,ignore_index.
concat函數(shù)的第一個參數(shù)為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時合并結果為相同列名的數(shù)據(jù),axis=1時為具有相同索引的數(shù)據(jù)
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函數(shù)
merge函數(shù)是pandas提供的一種數(shù)據(jù)庫式的合并方法。
on可以指定合并的列、索引,how則是與數(shù)據(jù)庫join函數(shù)相似,取值為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函數(shù)可以通過pandas.merge(df1,df2)、df1.merge(df2)兩種形式來實現(xiàn)兩個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函數(shù)
append函數(shù)是pandas針對DataFrame、Series等數(shù)據(jù)結構合并提供的函數(shù)。
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關于數(shù)據(jù)合并的官方文檔http://pandas.pydata.org/pandas-docs/stable/merging.html
把兩個dataframe合并成一個
1.merage
result = pd.merge(對象1, 對象2, on='key')
對象1 和 對象2分別為要合并的dataframe,key是在兩個dataframe都存在的列(類似于數(shù)據(jù)庫表中的主鍵)
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)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解使用python的logging模塊在stdout輸出的兩種方法
這篇文章主要介紹了詳解使用python的logging模塊在stdout輸出的相關資料,需要的朋友可以參考下2017-05-05python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法
下面小編就為大家分享一篇python 實現(xiàn)數(shù)組list 添加、修改、刪除的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04解決Django中調(diào)用keras的模型出現(xiàn)的問題
今天小編就為大家分享一篇解決Django中調(diào)用keras的模型出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08在python中計算ssim的方法(與Matlab結果一致)
這篇文章主要介紹了在python中計算ssim的方法(與Matlab結果一致),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12