Search code examples
pythonpandasrows

Python/Pandas- how to read_csv and as the same time ignore rows that have #?


enter image description here

My files have two formats ...some have # lines in the begining and some dont. I want to read_csv the matrix above into pandas dataframe and want to ignore the rows with # before populating my dataframe. My headers should be the ID SID and AID and so on.....so i think i can read a file by skipping the first 4 rows and i know how to do that. But the problem is there are files where the rows donot have first 4 # rows and directly start with ID SID AID....headers.

When i read in the data frame, i guess it assigns the col name as #PI


Solution

  • The pandas read_csv function allows you to specify a comment character via comment='#'. This will ignore any lines that begin with #.