最近在工作中,遇到了数据合并、连接的问题,故整理如下,供需要者参考~
参考自:象在舞:/gdkyxy/article/details/80785361
concat
concat:沿着一条轴,将多个对象堆叠到一起
concat方法相当于数据库中的全连接(union all),它不仅可以指定连接的方式(outer join或inner join)还可以指定按照某个轴进行连接。与数据库不同的是,它不会去重,但是可以使用drop_duplicates方法达到去重的效果。
concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True):
pd.concat()只是单纯的把两个表拼接在一起,参数axis是关键,它用于指定是行还是列,axis默认是0。当axis=0时,pd.concat([obj1, obj2])的效果与obj1.append(obj2)是相同的;当axis=1时,pd.concat([obj1, obj2], axis=1)的效果与pd.merge(obj1, obj2, left_index=True, right_index=True, how=‘outer’)是相同的。
参数介绍:
objs:需要连接的对象集合,一般是列表或字典;
axis:连接轴向;
join:参数为‘outer’或‘inner’;
join_axes=[]:指定自定义的索引;
keys=[]:创建层次化索引;
ignore_index=True:重建索引
案例:
student.csv
id name age sex0 1 tom 23 man1 2 john 33 man2 3 alice 22 woman3 4 jack 42 man4 5 saex 22 woman5 6 jmas 21 man6 7 jjban 34 man7 8 alicn 22 woman
score.csv
id name score0 1 tom891 2 john902 3 alice783 4 jack994 5 saex87
使用contract进行连接,注意contract([df1,df2]) 的这种写法,join可选outer/inner
contract_pd = pd.concat([student_pd,score_pd],join='outer', ignore_index=True)id name age sex score0 1 tom 23.0 man NaN1 2 john 33.0 man NaN2 3 alice 22.0 woman NaN3 4 jack 42.0 man NaN4 5 saex 22.0 woman NaN5 6 jmas 21.0 man NaN6 7 jjban 34.0 man NaN7 8 alicn 22.0 woman NaN0 1 tom NaN NaN 89.01 2 john NaN NaN 90.02 3 alice NaN NaN 78.03 4 jack NaN NaN 99.04 5 saex NaN NaN 87.05 6 jmas NaN NaN 33.0contract_pd = pd.concat([student_pd,score_pd],join='inner', ignore_index=True)id name0 1 tom1 2 john2 3 alice3 4 jack4 5 saex5 6 jmas6 7 jjban7 8 alicn8 1 tom9 2 john10 3 alice11 4 jack12 5 saex13 6 jmas
merge 通过键拼接列
类似于 关系型数据库 的连接方式,可以根据一个或多个键将不同的DatFrame连接起来。该函数的典型应用场景是,针对同一个主键存在两张不同字段的表,根据主键整合到一张表里面。
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:两个不同的DataFrame;
how:连接方式,有inner、left、right、outer,默认为inner;
on:指的是用于连接的列索引名称,必须存在于左右两个DataFrame中,如果没有指定且其他参数也没有指定,则以两个DataFrame列名交集作为连接键;
left_on:左侧DataFrame中用于连接键的列名,这个参数左右列名不同但代表的含义相同时非常的有用;
right_on:右侧DataFrame中用于连接键的列名;
left_index:使用左侧DataFrame中的行索引作为连接键;
right_index:使用右侧DataFrame中的行索引作为连接键;
sort:默认为True,将合并的数据进行排序,设置为False可以提高性能;
suffixes:字符串值组成的元组,用于指定当左右DataFrame存在相同列名时在列名后面附加的后缀名称,默认为(’_x’, ‘_y’);
copy:默认为True,总是将数据复制到数据结构中,设置为False可以提高性能;
示例:
# 1.默认以重叠的列名当做连接键contract_pd = pd.merge(student_pd,score_pd,how="inner",sort=True)id name_x age sex name_y score0 1 tom 23 man tom891 2 john 33 man john902 3 alice 22 woman alice783 4 jack 42 man jack994 5 saex 22 woman saex875 6 jmas 21 man jmas33# 2.默认做inner连接(取key的交集),连接方式还有(left,right,outer),制定连接方式加参数:how=''contract_pd = pd.merge(student_pd,score_pd,how="left",on='id',sort=True)id name_x age sex name_y score0 1 tom 23 man tom 89.01 2 john 33 man john 90.02 3 alice 22 woman alice 78.03 4 jack 42 man jack 99.04 5 saex 22 woman saex 87.05 6 jmas 21 man jmas 33.06 7 jjban 34 man NaN NaN7 8 alicn 22 woman NaN NaN# 3. 执行on 的时候不能够指定left_on 或者right_oncontract_pd = pd.merge(student_pd, score_pd, how="left", left_on='id', right_on='id', sort=True)id name_x age sex name_y score0 1 tom 23 man tom 89.01 2 john 33 man john 90.02 3 alice 22 woman alice 78.03 4 jack 42 man jack 99.04 5 saex 22 woman saex 87.05 6 jmas 21 man jmas 33.06 7 jjban 34 man NaN NaN7 8 alicn 22 woman NaN NaN
按照条件取出merge的结果:
# 取出score为NaN的记录allsed = contract_pd.loc[contract_pd.score.isna()]id name_x age sex name_y score6 7 jjban 34 man NaN NaN7 8 alicn 22 woman NaN NaN# 取出score 为非NaN的记录allsed = contract_pd.loc[~contract_pd.score.isna()]id name_x age sex name_y score0 1 tom 23 man tom 89.01 2 john 33 man john 90.02 3 alice 22 woman alice 78.03 4 jack 42 man jack 99.04 5 saex 22 woman saex 87.05 6 jmas 21 man jmas 33.0# 对结果进行去重allsed.drop_duplicates()
作用上lambda函数:
tag_pd = pd.read_csv("tags.csv")id tags0 1 1234|2345|3456|2348|7865|13571 2 1234|2345|3456|2348|7865|13572 3 1234|2345|3456|2348|7865|13573 4 1234|2345|3456|2348|7865|13574 5 1234|2345|3456|2348|7865|13575 6 1234|2345|3456|2348|7865|1357tag_pd['idss'] = tag_pd.tags.apply(lambda x:x.split('|'))id tags idss0 1 1234|2345|3456|2348|7865|1357 [1234, 2345, 3456, 2348, 7865, 1357]1 2 1234|2345|3456|2348|7865|1357 [1234, 2345, 3456, 2348, 7865, 1357]2 3 1234|2345|3456|2348|7865|1357 [1234, 2345, 3456, 2348, 7865, 1357]3 4 1234|2345|3456|2348|7865|1357 [1234, 2345, 3456, 2348, 7865, 1357]4 5 1234|2345|3456|2348|7865|1357 [1234, 2345, 3456, 2348, 7865, 1357]
总结:
contract 类似于关系型数据库中的union all 操作merge 类似于关系型数据库中的 inner join 、left join、right join 操作