-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.18 dataframe_concatenate.py
45 lines (36 loc) · 1.22 KB
/
3.18 dataframe_concatenate.py
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
# 连接2个数据帧
import pandas as pd
data_a = {'id':['1','2','3'],
'first':['Alex','Amy','Ali'],
'last':['Anderson','Ackerman','Allen']}
datafram_a = pd.DataFrame(data_a,columns=['id','first','last'])
data_b = {'id':['4','5','6'],
'first':['Billy','Brain','Bran'],
'last':['Bonder','Black','Balwner']}
datafram_b = pd.DataFrame(data_b,columns=['id','first','last'])
# --------------------按行的方向连接连个数据帧
df = pd.concat([datafram_a,datafram_b],axis=0)
print(df)
# id first last
# 0 1 Alex Anderson
# 1 2 Amy Ackerman
# 2 3 Ali Allen
# 0 4 Billy Bonder
# 1 5 Brain Black
# 2 6 Bran Balwner
# -----------------按列的方向连接连个数据帧
df = pd.concat([datafram_a,datafram_b],axis=1)
print(df)
# id first last id first last
# 0 1 Alex Anderson 4 Billy Bonder
# 1 2 Amy Ackerman 5 Brain Black
# 2 3 Ali Allen 6 Bran Balwner
# 附加一行
row = pd.Series([10,'Chris','Chillon'],index=['id','first','last'])
df = datafram_b.append(row,ignore_index=True)
print(df)
# id first last
# 0 4 Billy Bonder
# 1 5 Brain Black
# 2 6 Bran Balwner
# 3 10 Chris Chillon