python 把幾個DataFrame合並成一個DataFrame——merge,append,join,conca


原文 :https://blog.csdn.net/milton2017/article/details/54406482/

python  把幾個DataFrame合並成一個DataFrame——merge,append,join,conca

pandas provides various facilities for easily combining together Series, DataFrame, and Panel objects with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations.
 
1、merge
pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=('_x', '_y'), copy=True, indicator=False)
left 對象
right 另一個對象
on︰ 加入 (名稱)必須 綜合對象找到如果通過 left_index  right_index 推斷 DataFrames 交叉點連接
left_on 左邊綜合使用作為可以列名數組長度等於長度綜合
right_on 正確綜合用作可以列名數組長度等於長度綜合
left_index 如果為 True使用索引 (行標簽) 綜合作為聯接鍵。多重 (層次) 的綜合級別必須匹配聯接綜合數目
right_index 相同用法作為正確綜合 left_index
how︰ 之一 '左','右','外在'、 '內部'。默認內部每個方法詳細說明參閱
sort︰ 綜合通過聯接字典順序結果進行排序默認值為 True設置 False提高性能極大地許多情況下
suffixes︰ 字符串后綴不適用於重疊元組默認值 ('_x','_y')。
copy︰  即使重新索引必要總是傳遞綜合對象復制數據 (默認值True)許多情況下不能避免可能會提高性能 / 內存使用情況可以避免復制上述案件有些病理盡管如此提供選項
indicator︰ 將添加輸出綜合呼吁 _merge 信息一行_merge 絕對類型觀測合並出現 '左' 的綜合觀測合並會出現 '正確' 的綜合兩個如果觀察合並關鍵發現兩個 right_only left_only 的
 
1.result = pd.merge(left, right, on='key')
2.result = pd.merge(left, right,on=['key1', 'key2'])
 
         
3.result = pd.merge(left, right, how='left', on=['key1', 'key2'])
4.result = pd.merge(left, right, how='right', on=['key1', 'key2'])
5.result = pd.merge(left, right, how='outer', on=['key1', 'key2'])
 
2、append
1.result = df1.append(df2)
2.result = df1.append(df4)
3.result = df1.append([df2, df3])
4.result = df1.append(df4, ignore_index=True)
 
4、join
left.join(right, on=key_or_keys) pd.merge(left, right, left_on=key_or_keys, right_index=True, how='left', sort=False)
1.result = left.join(right, on='key')
2.result = left.join(right, on=['key1', 'key2'])
3.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)
objs︰ 一個序列系列 綜合面板對象映射如果字典傳遞作為參數使用排序除非傳遞種情況下選擇 (見下文)。任何沒有任何反對默默地丟棄除非他們沒有種情況下引發 ValueError
axis: {0,1,...},默認值為 0。連接沿
join: {'內部'、 '外'},默認 '外'。如何處理其他 axis(es) 索引聯盟 交叉口
ignore_index 布爾值 默認 False如果為 True不要串聯使用索引由此產生標記 0,...n-1。有用如果串聯串聯沒有有意義索引信息對象請注意聯接仍然受到尊重其他索引
join_axes 索引對象列表具體指標用於其他 n-1 而不是執行內部/外部設置邏輯
keys︰  序列默認構建分層索引使用通過作為外面級別如果多個級別獲得通過包含元組
levels 列表序列默認具體水平 (唯一值) 用於構建多重否則他們推斷鑰匙
names 列表中默認由此產生分層索引名稱
verify_integrity 布爾值 默認 False檢查是否串聯包含重復項可以相對於實際數據串聯非常昂貴
副本  布爾值 默認 True如果為 False不要不必要地復制數據

1.frames = [df1, df2, df3] 2.result = pd.concat(frames)
 
3.result = pd.concat(frames, keys=['x', 'y', 'z'])
4.result.ix['y']
    A   B   C   D
4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7
5.result = pd.concat([df1, df4], axis=1)
6.result = pd.concat([df1, df4], axis=1, join='inner')
7.result = pd.concat([df1, df4], axis=1, join_axes=[df1.index])
8.result = pd.concat([df1, df4], ignore_index=True)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM