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

最近在倒腾一个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
'''

代码图片


版权声明:本文为weixin_40283454原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。