按列合并
(效果和增加列相同)
df4 = pd.DataFrame({'address':['school','home','school','school','home']})
df5 = pd.concat([df2,df4],axis=1)
print('合并前的df2\n',df2)
print('合并前的df4\n',df4)
print('合并后的df5\n',df5)
合并前的df2
name no age gender new_Col
0 Tom 001 16 m 1
1 Lily 002 16 f 2
2 Cindy 003 15 f 3
3 Petter 004 16 m 4
4 Stark 005 15 m 5
合并前的df4
address
0 school
1 home
2 school
3 school
4 home
合并后的df5
name no age gender new_Col address
0 Tom 001 16 m 1 school
1 Lily 002 16 f 2 home
2 Cindy 003 15 f 3 school
3 Petter 004 16 m 4 school
4 Stark 005 15 m 5 home
按行合并
效果和增加学生信息相同
df6 = pd.DataFrame({'name':['Tony'],'no':['005'],'age':[16],'gender':['m']})
df7 = pd.concat([df1,df6],axis=0)
print('合并前的df1\n',df1)
print('合并前的df6\n',df6)
print('合并后的df7\n',df7)
合并前的df1
name no age gender
id
0 Tom 001 16 m
1 Lily 002 16 f
2 Cindy 003 15 f
3 Petter 004 16 m
合并前的df6
name no age gender
0 Tony 005 16 m
合并后的df7
name no age gender
0 Tom 001 16 m
1 Lily 002 16 f
2 Cindy 003 15 f
3 Petter 004 16 m
0 Tony 005 16 m