Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip header and footer data in pandas dataframe?

Tags:

python

pandas

I have first 15 rows of a excel file as "Header data". and after 235 rows, "Footer data". I need to read data in between these header and footer data.

Is there any way to read data into DataFrame by selecting specific range of rows using pandas?

like image 397
Yami Danchou Avatar asked Aug 16 '17 21:08

Yami Danchou


People also ask

How do I skip a header in a data frame?

You can set the header option to None to ignore header.

How do I remove a header from a Dataframe in Python?

In order to export pandas DataFrame to CSV without index (no row indices) use param index=False and to ignore/remove header use header=False param on to_csv() method.

How do I read a CSV file without header in Python?

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.


1 Answers

Demo:

xl = pd.ExcelFile(filepath)

# parsing first (index: 0) sheet
total_rows = xl.book.sheet_by_index(0).nrows

skiprows = 15
nrows = 235 - 15

# calc number of footer rows
# (-1) - for the header row
skipfooter = total_rows - nrows - skiprows - 1

df = xl.parse(0, skiprows=skiprows, skipfooter=skipfooter)
like image 55
MaxU - stop WAR against UA Avatar answered Sep 17 '22 14:09

MaxU - stop WAR against UA