yyhhyy's blog

yyhhyy

5.9 高级处理-合并

46
2023-06-09

5.9 高级处理-合并

学习目标

  • 目标

    • 应用pd.concat实现数据的合并

    • 应用pd.merge实现数据的合并


如果你的数据由多张表组成,那么有时候需要将不同的内容合并在一起分析

1 pd.concat实现数据合并

  • pd.concat([data1, data2], axis=1)

    • 按照行或列进行合并,axis=0为列索引,axis=1为行索引

比如我们将刚才处理好的one-hot编码与原数据合并

# 按照行索引进行
pd.concat([data, dummies], axis=1)

2 pd.merge

  • pd.merge(left, right, how='inner', on=None)

    • 可以指定按照两组数据的共同键值对合并或者左右各自

    • left: DataFrame

    • right: 另一个DataFrame

    • on: 指定的共同键

    • how:按照什么方式连接

Merge method

SQL Join Name

Description

left

LEFT OUTER JOIN

Use keys from left frame only

right

RIGHT OUTER JOIN

Use keys from right frame only

outer

FULL OUTER JOIN

Use union of keys from both frames

inner

INNER JOIN

Use intersection of keys from both frames

2.1 pd.merge合并

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']})

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

result
  key1 key2   A   B   C   D
0   K0   K0  A0  B0  C0  D0
1   K1   K0  A2  B2  C1  D1
2   K1   K0  A2  B2  C2  D2

  • 左连接

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

result
  key1 key2   A   B    C    D
0   K0   K0  A0  B0   C0   D0
1   K0   K1  A1  B1  NaN  NaN
2   K1   K0  A2  B2   C1   D1
3   K1   K0  A2  B2   C2   D2
4   K2   K1  A3  B3  NaN  NaN

  • 右连接

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

result
  key1 key2    A    B   C   D
0   K0   K0   A0   B0  C0  D0
1   K1   K0   A2   B2  C1  D1
2   K1   K0   A2   B2  C2  D2
3   K2   K0  NaN  NaN  C3  D3

  • 外链接

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

result
  key1 key2    A    B    C    D
0   K0   K0   A0   B0   C0   D0
1   K0   K1   A1   B1  NaN  NaN
2   K1   K0   A2   B2   C1   D1
3   K1   K0   A2   B2   C2   D2
4   K2   K1   A3   B3  NaN  NaN
5   K2   K0  NaN  NaN   C3   D3

3 总结

  • pd.concat([数据1, 数据2], axis=**)【知道】

  • pd.merge(left, right, how=, on=)【知道】

    • how -- 以何种方式连接

    • on -- 连接的键的依据是哪几个