Pandas数据读写全攻略
1. 文本文件读取技巧
在处理文本文件时,我们常常会遇到需要排除某些行进行解析的情况。比如文件中的标题行或者不必要的注释行,我们可以使用 skiprows 选项来实现这一需求。使用时要注意,若想排除前五行,应写成 skiprows = 5 ;若只想排除第五行,则需写成 skiprows = [5] 。
以下是一个示例文件 ch05_06.txt :
########### LOG FILE ############
This file has been generated by automatic system
white,red,blue,green,animal
12-Feb-2015: Counting of animals inside the house
1,5,2,3,cat
2,7,8,5,dog
13-Feb-2015: Counting of animals outside the house
3,3,6,7,horse
2,2,8,3,duck
4,4,2,1,mouse
使用 read_table 函数排除指定行:
>>> read_table('ch05_06.txt',sep=',',skiprows=[0,1,3,6])
white red blue green
超级会员免费看
订阅专栏 解锁全文
1055

被折叠的 条评论
为什么被折叠?



