1.merge

  • 合并时有4种方法how = [‘left’, ‘right’, ‘outer’, ‘inner’],预设值how=’inner’。

  • indicator=True会将合并的记录放在新的一列。indicator=’indicator_column’修改indicator生成的列名

8.多表联合 - 图1

Student_Score.xlsx

  1. import pandas as pd
  2. students = pd.read_excel('tmp1\Student_Score.xlsx', sheet_name='Students')
  3. scores = pd.read_excel('tmp1\Student_Score.xlsx', sheet_name='Scores')
  4. tables = students.merge(scores, how='left', on='ID').fillna(0)
  5. # 默认inner join 内连,两个表进行数据联立时,如果联立不上就丢弃数据
  6. # 如果两个表没有相同的ID 就设置left_on= right_on=
  7. # left 意思就是无论条件成不成立 都保留左边表的数据 则保留students表的数据
  8. # fillna()指的是用什么来替代NaN
  9. tables.Score = tables.Score.astype(int) # 将Score那一列的数据变为int整型
  10. print(tables)
  11. """
  12. ID Name Score
  13. 0 1 Student_001 81
  14. 1 3 Student_003 83
  15. 2 5 Student_005 85
  16. 3 7 Student_007 87
  17. 4 9 Student_009 89
  18. 5 11 Student_011 91
  19. 6 13 Student_013 93
  20. 7 15 Student_015 95
  21. 8 17 Student_017 97
  22. 9 19 Student_019 99
  23. 10 21 Student_021 0
  24. 11 23 Student_023 0
  25. 12 25 Student_025 0
  26. 13 27 Student_027 0
  27. 14 29 Student_029 0
  28. 15 31 Student_031 0
  29. 16 33 Student_033 0
  30. 17 35 Student_035 0
  31. 18 37 Student_037 0
  32. 19 39 Student_039 0
  33. """

2.join

  • join在进行左右联立表时,会默认用index进行联立,join有on参数 ,但是去掉了left_on和right_on参数
  1. import pandas as pd
  2. students = pd.read_excel('tmp1\Student_Score.xlsx', sheet_name='Students', index_col='ID')
  3. scores = pd.read_excel('tmp1\Student_Score.xlsx', sheet_name='Scores', index_col='ID')
  4. tables = students.join(scores, how='left').fillna(0)
  5. # 默认inner join 内连,两个表进行数据联立时,如果联立不上就丢弃数据
  6. tables.Score = tables.Score.astype(int) # 将Score那一列的数据变为int整型
  7. print(tables)

3.concat

  • concat将两张表串联起来默认从上到下0,

  • 可以将数据根据不同的轴作简单的融合

  • 参数说明
    objs: series,dataframe或者是panel构成的序列list
    axis: 需要合并链接的轴,0是行,1是列
    join:连接的方式 inner,或者outer,join=’outer’为预设值,因此未设定任何参数时,函数默认join=’outer’。此方式是依照column来做纵向合并,有相同的column上下合并在一起,其他独自的column个自成列,原本没有值的位置皆以NaN填充。

join = ‘innner’表示只有相同的column合并在一起,其他的会被抛弃。

Students16.xlsx

  1. import pandas as pd
  2. import numpy as np
  3. page_001 = pd.read_excel('tmp1\Students16.xlsx', sheet_name='Page_001')
  4. page_002 = pd.read_excel('tmp1\Students16.xlsx', sheet_name='Page_002')
  5. # concat将两张表串联起来默认从上到下0
  6. students = pd.concat([page_001, page_002]).reset_index(drop=True)
  7. print(students)
  8. # reset_index()重置index,drop=Ture删去原index
  9. """
  10. ID Name Score
  11. 0 1 Student_001 90
  12. 1 2 Student_002 90
  13. 2 3 Student_003 90
  14. 3 4 Student_004 90
  15. 4 5 Student_005 90
  16. 5 6 Student_006 90
  17. 6 7 Student_007 90
  18. 7 8 Student_008 90
  19. 8 9 Student_009 90
  20. 9 10 Student_010 90
  21. 10 11 Student_011 90
  22. 11 12 Student_012 90
  23. 12 13 Student_013 90
  24. 13 14 Student_014 90
  25. 14 15 Student_015 90
  26. 15 16 Student_016 90
  27. 16 17 Student_017 90
  28. 17 18 Student_018 90
  29. 18 19 Student_019 90
  30. 19 20 Student_020 90
  31. 20 21 Student_021 80
  32. 21 22 Student_022 80
  33. 22 23 Student_023 80
  34. 23 24 Student_024 80
  35. 24 25 Student_025 80
  36. 25 26 Student_026 80
  37. 26 27 Student_027 80
  38. 27 28 Student_028 80
  39. 28 29 Student_029 80
  40. 29 30 Student_030 80
  41. 30 31 Student_031 80
  42. 31 32 Student_032 80
  43. 32 33 Student_033 80
  44. 33 34 Student_034 80
  45. 34 35 Student_035 80
  46. 35 36 Student_036 80
  47. 36 37 Student_037 80
  48. 37 38 Student_038 80
  49. 38 39 Student_039 80
  50. 39 40 Student_040 80
  51. """

4.append()

append是series和dataframe的方法,使用它就是默认沿着列进行凭借(axis = 0,列对齐)

  1. # pd.append(data2) # 在数据框data2的末尾添加数据框data1,其中data1和data2的列数应该相等

成绩最值比较(最好的成绩与最差的成绩)

  1. print(data.sort_values('Score').head(1).append(data.sort_values('Score').tail(1)))
  2. """
  3. ID Name Age Score
  4. 10 11 Student_011 22 50
  5. 2 3 Student_003 33 100
  6. """