日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

python中DataFrame數(shù)據(jù)合并merge()和concat()方法詳解_python

作者:侯小啾 ? 更新時間: 2022-08-27 編程語言

merge()

1.常規(guī)合并

①方法1

指定一個參照列,以該列為準,合并其他列。

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

要實現(xiàn)該合并,也可以通過索引來合并,即以index列為基準。將left_index 和 right_index 都設(shè)置為True
即可。(left_index 和 right_index 都默認為False,left_index表示左表以左表數(shù)據(jù)的index為基準, right_index表示右表以右表數(shù)據(jù)的index為基準。)

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 左表,合并對象,DataFrame或Series
right 右表,合并對象,DataFrame或Series
how 合并方式,可以是left(左合并), right(右合并), outer(外合并), inner(內(nèi)合并)
on 基準列 的列名
left_on 左表基準列列名
right_on 右表基準列列名
left_index 左列是否以index為基準,默認False,否
right_index 右列是否以index為基準,默認False,否

其中,left_index與right_index 不能與 on 同時指定。

合并方式 left right outer inner

準備數(shù)據(jù)‘

新準備一組數(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(默認)

使用來自兩個數(shù)據(jù)集的鍵的交集

df_merge = pd.merge(df1, df2, on='id')
print(df_merge)

outer

使用來自兩個數(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.多對一合并

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é)果如圖所示:

依然按照默認的Inner方式,使用來自兩個數(shù)據(jù)集的鍵的交集。且重復(fù)的鍵的行會在合并結(jié)果中體現(xiàn)為多行。

3.多對多合并

如圖表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對象的序列或映射
axis 默認為0,表示列。如果為1則表示行。
join 默認為"outer",也可以為"inner"
ignore_index 默認為False,表示保留索引(不忽略)。設(shè)為True則表示忽略索引。

其他重要參數(shù)通過實例說明。

1.相同字段的表首位相連

首先準備三組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)

如果想要在合并后,標記一下數(shù)據(jù)都來自于哪張表或者數(shù)據(jù)的某類別,則也可以給concat加上 參數(shù)keys

result = pd.concat(dfs, keys=['table1', 'table2', 'table3'])
print(result)

此時,添加的keys與原來的index組成元組,共同成為新的index。

print(result.index)

2.橫向表合并(行對齊)

準備兩組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)

當axis為默認值0時:

result = pd.concat([df1, df2])
print(result)

橫向合并需要將axis設(shè)置為1

result = pd.concat([df1, df2], axis=1)
print(result)

對比以上輸出差異。

  • axis=0時,即默認縱向合并時,如果出現(xiàn)重復(fù)的行,則會同時體現(xiàn)在結(jié)果中
  • axis=1時,即橫向合并時,如果出現(xiàn)重復(fù)的列,則會同時體現(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

欄目分類
最近更新