1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.randint(0, 20, (3,4)),
columns=['col1','col2','col3','col4'],
index=['sp1','sp2','sp3'])
# col1 col2 col3 col4
# sp1 11 10 19 13
# sp2 6 13 18 10
# sp3 5 14 6 10
df2 = pd.DataFrame(np.random.randint(0, 20, (2,4)),
columns=['col1','col2','col3','col4'],
index=['sp4','sp5'])
# col1 col2 col3 col4
# sp4 11 2 0 2
# sp5 11 19 12 4
df3 = pd.DataFrame(np.random.randint(0, 20, (2,4)),
columns=['col1','col2','col3','col4'],
index=['sp3','sp4'])
# col1 col2 col3 col4
# sp3 13 5 3 15
# sp4 7 2 2 13
df4 = pd.DataFrame(np.random.randint(0, 20, (2,4)),
columns=['col3','col4','col5','col6'],
index=['sp4','sp5'])
# col3 col4 col5 col6
# sp4 11 1 2 15
# sp5 18 2 5 18
df5 = pd.DataFrame(np.random.randint(0, 6, (3,2)),
columns=['col5','col6'],
index=['sp1','sp2','sp3'])
# col5 col6
# sp1 0 5
# sp2 5 3
# sp3 2 0
##(1)普通合并--列名相同,行名不同,上下合并
pd.concat([df1, df2])
##(2)普通合并--行名相同,列名不同,左右合并
pd.concat([df1, df5], axis=1)
##(3)上下合并时,忽略索引重复
pd.concat([df1, df3]) #允许存在重复行名
pd.concat([df1, df3]).reset_index()
pd.concat([df1, df3], verify_integrity=True) #有重复行名会报错
pd.concat([df1, df3], ignore_index=True) #重命名所有列名去重
##(4)上下合并时,列名不完全一致
pd.concat([df1, df4]) #默认保留全集列名
pd.concat([df1, df4], join='inner') #仅保留交集索引
|