Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas read_csv() conditionally skipping header row

Tags:

python

pandas

csv

I'm trying to read a csv file but my csv files differ. Some have different format and some have other. I'm trying to add controls so that I will not need to edit my code or my input file.

My problem is, some of these csv files have a line of String above the column headers. An example:

Created on 12-11-2018,CryptoDataDownload.com
Date,Symbol,Open,High,Low,Close,Volume From,Volume To
2018-12-11 11-AM,ADABTC,8.6e-06,8.61e-06,8.55e-06,8.57e-06,301141.7,2.59
2018-12-11 10-AM,ADABTC,8.69e-06,8.72e-06,8.6e-06,8.6e-06,236949.63,2.05

If I import this, the delimeter will use the first line and separate the file into two columns as Created on 12-11-2018 and CryptoDataDownload.com.

This is how df.head() looks like:

                        Created on 12-11-2018 CryptoDataDownload.com
Date             Symbol Open     High     Low      Close              Volume From                          Volume To
2018-12-11 11-AM ADABTC 8.6e-06  8.61e-06 8.55e-06 8.57e-06              301141.7                               2.59
2018-12-11 10-AM ADABTC 8.69e-06 8.72e-06 8.6e-06  8.6e-06              236949.63                               2.05
2018-12-11 09-AM ADABTC 8.7e-06  8.7e-06  8.62e-06 8.69e-06             509311.39                               4.41
2018-12-11 08-AM ADABTC 8.69e-06 8.7e-06  8.63e-06 8.7e-06              111367.34                             0.9656

I want to check if this file has this line and skip it if so.

How can I do this?

like image 861
iso_9001_ Avatar asked Jan 31 '26 23:01

iso_9001_


1 Answers

If the headers in your CSV files follow a similar pattern, you can do something simple like sniffing out the first line before determining whether to skip the first row or not.

filename = '/path/to/file.csv'
skiprows = int('Created in' in next(open(filename)))
df = pd.read_csv(filename, skiprows=skiprows)

Good pratice would be to use a context manager, so you could also do this:

filename = '/path/to/file.csv'
skiprows = 0
with open(filename, 'r+') as f:
    for line in f:
        if line.startswith('Created '):
            skiprows = 1
        break
df = pd.read_csv(filename, skiprows=skiprows)
like image 111
cs95 Avatar answered Feb 03 '26 12:02

cs95