最近在倒腾一个txt文件,因为文件太大,所以给切割成了好几个小的文件,只有第一个文件有标题,从第二个开始就没有标题了。
我的需求是取出指定的列的数据,踩了些坑给研究出来了。

import pandas as pd
# 我们的需求是 取出所有的姓名
# test1的内容
'''
id name score
1   张三 100
2   李四  99
3   王五  98
'''
test1 = pd.read_table("test1.txt")  # 这个是带有标题的文件
names = test1["name"]  # 根据标题来取值
print(names)
'''
张三
李四
王五
'''


# test2的内容
'''
4   Allen 100
5   Bob  99
6   Candy 98
'''
test2 = pd.read_table("test2.txt", header=None)  # 这个是没有标题的文件
names = test2[1]  # 根据index来取值 
print(names)
'''
Allen
Bob
Candy
'''

使用pandas读取txt文件中的指定的列(有无标题)

相关文章:

  • 2021-05-20
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-09-16
猜你喜欢
  • 2021-08-12
  • 2021-11-07
  • 2021-03-26
  • 2022-12-23
  • 2021-04-17
  • 2022-12-23
  • 2021-08-10
相关资源
相似解决方案