Pandas concat和merge合并

1、pd.concat实现数据合并

def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
           keys=None, levels=None, names=None, verify_integrity=False,
           sort=None, copy=True):
# objs: series,dataframe或者是panel构成的序列lsit
# axis: 需要合并的轴,0是行,1是列
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                   'D': ['D0', 'D1', 'D2', 'D3']},
                    index=[0, 1, 2, 3])


df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                    'B': ['B4', 'B5', 'B6', 'B7'],
                   'C': ['C4', 'C5', 'C6', 'C7'],
                    'D': ['D4', 'D5', 'D6', 'D7']},
                     index=[4, 5, 6, 7])

frames = [df1, df2]
result = pd.concat(frames,axis=0)
print(result)
 # join:连接的方式 inner,或者outer
# inner交集 or outer并集,在并集上原来没有元素的片上用np.nan填充
# keys属性,指定数据来自哪一个分片
result = pd.concat(frames, keys=['x', 'y'])
print(result)
# 指定具体用哪个分片的index合并
result = pd.concat([df1, df4], axis=1, join_axes=[df1.index])

2、pd.merge

def 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,
         validate=None):
# left,right:需要merge的DataFrame对象
# how:jion的方式,inner(默认),outer,left(左边对象的key),或者right
# on:jion用来对齐的那一列的名字,Columns (names) to join on. Must be found in both the left and right DataFrame objects.
#
left_on=None, right_on=None:指定左右键
 
left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                        'key2': ['K0', 'K1', 'K0', 'K1'],
                        'A': ['A0', 'A1', 'A2', 'A3'],
                        'B': ['B0', 'B1', 'B2', 'B3']})

right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                        'key2': ['K0', 'K0', 'K0', 'K0'],
                        'C': ['C0', 'C1', 'C2', 'C3'],
                        'D': ['D0', 'D1', 'D2', 'D3']})

2.1、内连接,默认为内连接

# 默认内连接
result = pd.merge(left, right, on=['key1', 'key2'])    # 交集

2.2、左连接

result = pd.merge(left, right, how='left', on=['key1', 'key2'])

2.3、右连接

result = pd.merge(left, right, how='right', on=['key1', 'key2'])

2.4、外链接

result = pd.merge(left, right, how='outer', on=['key1', 'key2'])    # 并集

3、pd.join

left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
                     'B': ['B0', 'B1', 'B2'],
                    "key":['K0', 'K1', 'K2']})

right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],
                      'D': ['D0', 'D2', 'D3']},
                     index=['K0', 'K2', 'K3'])

# 默认内连接
result = left.join(right,on="key")
print(result)
# 等同于如下
result = pd.merge(left, right, left_on='key', right_index=True,how='left', sort=False)
print(result)

详细链接:https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

 

猜你喜欢

转载自www.cnblogs.com/caijunchao/p/12906588.html