test.py 740 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/8/3 9:24
  4. # file: test.py
  5. # author: David
  6. # company: shenyang JY
  7. import pandas as pd
  8. data = [[45, 65, 100], [56, 45, 50], [67, 68, 98]]
  9. index = [['张三', '李四', '王五']]
  10. columns = ['数学', '语文', '英语']
  11. df = pd.DataFrame(data=data, index=index, columns=columns)
  12. # 使用列名提取
  13. print(df[['数学', '英语']])
  14. print('------------------------------')
  15. print(df.loc[:, ['数学', '英语']]) # 逗号左侧行,右侧列
  16. print('------------------------------')
  17. print(df.iloc[:, [0, 2]])
  18. print('------------------------------')
  19. # 提取连续的列 从语文到最后
  20. print(df.loc[:, '语文':])
  21. print('------------------------------')
  22. print(df.iloc[:, 1:])