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.

read_excel return empty dataframe when using usecols

See original GitHub issue
In [3]: data = pd.read_excel("A.xlsx")

In [4]: data
Out[4]:
   A  B
0  1  2
1  3  4

In [5]: data1 = pd.read_excel("A.xlsx",usecols=['B'])

In [6]: data1
Out[6]:
Empty DataFrame
Columns: []
Index: []

In [7]: pd.__version__
Out[7]: '0.21.0'

Problem description

Having a excel file name A.xlsx(or A.xls) with column A,B read_excel return empty dataframe if usecols used

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:32 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
LISHITINGcommented, Mar 23, 2018

For me Instead of typing usecols=['B'], try usecols='B'

2reactions
chris-b1commented, Mar 26, 2018

Copying my comment from #20480, welcome any feedback on how this api should work

#18273 is actually two somewhat separate problems and we have a bit of an API tangle here

  1. passing a list of Excel column letters doesn’t work (this fixes that)
  2. passing a list column names (e.g. column title 'foo' in the spreadsheet) to usecols doesn’t work anymore

Number 2 wasn’t part of the read_excel documented args, but once worked, because this param used to be called parse_cols (#17774), and if you passed something to usecols it would get passed down to the TextParser logic and do the right thing https://github.com/pandas-dev/pandas/blob/1915ffc53ea60494f24d83844bbff00efa392c82/pandas/io/excel.py#L529

Not sure what the solution is, there could be ambiguous cases (column titled'A' in spreadsheet column B), that would be easiest to handle with separate kwargs. Or could just have usecols do everything and warn in cases of ambiguity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - pandas,read_excel, usecols with list input generating ...
A possible approach is to read few rows and find the location of the column and then read the file second time. import...
Read more >
pandas read_excel usecols empty dataframe
To read an excel file as a DataFrame, use the pandas read_excel() method. ... entry As mentioned read_excel returns an empty DataFrame when...
Read more >
Pandas read_excel() - Reading Excel File in Python
We can use the pandas module read_excel() function to read the excel file data into a DataFrame object. If you look at an...
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 — pandas 1.5.2 documentation
Read an Excel file into a pandas DataFrame. ... If a subset of data is selected with usecols , index_col is based on...
Read more >

github_iconTop Related Medium Post

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