Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get one specific line of comment as header with python Pandas

I have a file looking like

# Comment 1
# Comment 2
# A B C
1 2 3 
4 5 6
7 8 9

How to read it with python pandas module, so as the last line of comments can be interpreted as the columns titles ?

I've tried

pandas.read_table(file_path, header= 2 , comment='#' )

But the comment lines are eliminated first, thus the header line will be 7 8 9

like image 826
Covich Avatar asked Feb 01 '26 02:02

Covich


2 Answers

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 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.)

like image 79
B. M. Avatar answered Feb 03 '26 10:02

B. M.


You can do this manually: first read the comments, parse the column names, then call read_table:

import itertools
import pandas as pd

def read_data(path):
    with open(path) as handle:
        *_comments, names = itertools.takewhile(
            lambda line: line.startswith('#'), handle)

        # This is not the most robust way, adjust for your needs :)
        names = names[1:].split()

    return pandas.read_table(path, header=0, names=names, sep=' ', comment='#')
like image 45
Sergei Lebedev Avatar answered Feb 03 '26 09:02

Sergei Lebedev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!