question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pandas read_excel: only read first few lines

See original GitHub issue

Code Sample, a copy-pastable example if possible

workbook_dataframe = pd.read_excel(workbook_filename, nrows = 10)

Problem description

Using pandas read_excel on about 100 excel files - some are large - I want to read the first few lines of each (header and first few rows of data).

The above doesn’t work but illustrates the goal (example reading 10 data rows).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
gmlandercommented, Sep 25, 2017

To get nrows without reading the entire worksheet:

workbook = pd.ExcelFile(workbook_filename)

# get the total number of rows (assuming you're dealing with the first sheet)
rows = workbook.book.sheet_by_index(0).nrows

# define how many rows to read
nrows = 10

# subtract the number of rows to read from the total number of rows (and another 1 for the header)
workbook_dataframe = pd.read_excel(workbook, skip_footer = (rows - nrows - 1))
4reactions
ppritish51commented, Jun 25, 2017

You can add one line in your code below where you are reading your file. For example, if you want to read first 10 rows of the file then you can do this.

workbook_dataframe = pd.read_excel(workbook_filename) workbook_dataframe =workbook_dataframe.iloc[:10]

or even you can simply do this workbook_dataframe = pd.read_excel(workbook_filename).iloc[:10]

so that your data frame now contains only first 10 rows.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas read_excel: only read first few lines - Stack Overflow
Problem with the workaround is it has to read the entire excel file before taking the head. I've also tried experimenting with skiprows...
Read more >
pandas.read_excel — pandas 1.5.2 documentation
Read an Excel file into a pandas DataFrame. Supports xls , xlsx , xlsm , xlsb , odf , ods and odt file...
Read more >
Pandas - read_excel() - How to read Excel file in python
Let's say we want to skip the first 2 rows when reading the file. df = pd.read_excel('reading_excel_file.xlsx', sheet_name='Purchase Orders 1', ...
Read more >
Pandas Read Excel with Examples
Use pandas.read_excel() function to read excel sheet into pandas DataFrame, by default it loads the first sheet from the excel file and parses...
Read more >
Pandas read_excel() - Reading Excel File in Python
We can specify the column names to be read from the excel file. It's useful when you are interested in only a few...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found