按列合并

(效果和增加列相同)

  1. df4 = pd.DataFrame({'address':['school','home','school','school','home']})
  2. df5 = pd.concat([df2,df4],axis=1)
  3. print('合并前的df2\n',df2)
  4. print('合并前的df4\n',df4)
  5. print('合并后的df5\n',df5)
  1. 合并前的df2
  2. name no age gender new_Col
  3. 0 Tom 001 16 m 1
  4. 1 Lily 002 16 f 2
  5. 2 Cindy 003 15 f 3
  6. 3 Petter 004 16 m 4
  7. 4 Stark 005 15 m 5
  8. 合并前的df4
  9. address
  10. 0 school
  11. 1 home
  12. 2 school
  13. 3 school
  14. 4 home
  15. 合并后的df5
  16. name no age gender new_Col address
  17. 0 Tom 001 16 m 1 school
  18. 1 Lily 002 16 f 2 home
  19. 2 Cindy 003 15 f 3 school
  20. 3 Petter 004 16 m 4 school
  21. 4 Stark 005 15 m 5 home

按行合并

效果和增加学生信息相同

  1. df6 = pd.DataFrame({'name':['Tony'],'no':['005'],'age':[16],'gender':['m']})
  2. df7 = pd.concat([df1,df6],axis=0)
  3. print('合并前的df1\n',df1)
  4. print('合并前的df6\n',df6)
  5. print('合并后的df7\n',df7)
  1. 合并前的df1
  2. name no age gender
  3. id
  4. 0 Tom 001 16 m
  5. 1 Lily 002 16 f
  6. 2 Cindy 003 15 f
  7. 3 Petter 004 16 m
  8. 合并前的df6
  9. name no age gender
  10. 0 Tony 005 16 m
  11. 合并后的df7
  12. name no age gender
  13. 0 Tom 001 16 m
  14. 1 Lily 002 16 f
  15. 2 Cindy 003 15 f
  16. 3 Petter 004 16 m
  17. 0 Tony 005 16 m