获得一条特定的注释行作为python Pandas的标题 [英] get one specific line of comment as header with python Pandas
本文介绍了获得一条特定的注释行作为python Pandas的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像
# Comment 1
# Comment 2
# A B C
1 2 3
4 5 6
7 8 9
如何可以使用python pandas模块读取它,因此注释的最后一行可以解释为列标题?
How to read it with python pandas module, so as the last line of comments can be interpreted as the columns titles ?
我已经尝试过
pandas.read_table(file_path, header= 2 , comment='#' )
但是首先删除注释行,因此标题行将为
7 8 9
But the comment lines are eliminated first, thus the header line will be
7 8 9
推荐答案
In [7]: pd.read_csv('test.csv',skiprows=2,sep='\s+',escapechar='#')
Out[7]:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
escapechar
告诉#
必须视为字段的结尾。在这里,它用作干净的解决方法。此处需要 sep ='\s +'
,因为文件(或此页面)中的3和6尾随空格。
escapechar
tell that #
must be consider as a end of field. Here it is used as a clean workaround. sep='\s+'
is required here because you have trailing space after 3 and 6 in your file (or this page.)
这篇关于获得一条特定的注释行作为python Pandas的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文