0

I have an excel with an explanation written in the first row as follows: I want to convert the second row as a header and the data below it into a dataframe format. So I wrote the following code, but the result is an empty data frame

df = pd.read_excel(filename, skiprows=1)
print(df) =
Empty DataFrame
Columns: []
Index: []

If I enter Excel and delete the first row and do not use skiprows, a correct dataframe appears. Which should I fix? enter image description here

And since there is only one sheet, I did not set it

When opening the first file saved, the cursor is positioned at row=1 column[a]=1. If I change the cursor position and save the excel, the data frame comes out well. How do I move the cursor and save it?

3 Answers 3

1

skiprows=1 works fine me.

pd.read_excel('book1.xlsx', skiprows=1)

Here is an alternative using openpyxl, maybe you can give this a try and see -

from openpyxl import load_workbook
wb = load_workbook('book1.xlsx')
ws = wb.get_sheet_by_name('Sheet1')
data = []
for row in ws.values:
    data.append([item for item in row])
df = pd.DataFrame(data[2:], columns=data[1])
Sign up to request clarification or add additional context in comments.

1 Comment

The first method failed, but the second method succeeded. Thank you
0

have you tried skiprows=[0]? This is outlined here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

Then the default behaviour for headers should be taken as the new row 0

11 Comments

I tried it the way I said, but I got an empty data frame
In my opinion, the data frame seems to read 1 row because A, B, C of 1 row are merged. So it seems that there is no data when I skiprows
without using skiprows, if you do a describe() on the dataframe, what is the result. since you also are not uploading your data, can you send an image of the resulting df without skiprows?
df = pd.read_excel(filename) print(df) = Empty DataFrame Columns: [1.excelexplanation... 2.... 3... ] Index: []
It seems that only the first row of excel is being read. The rest of the rows are not visible
|
0

The skiprow argument looks at rows starting from 0. So you have to put skiprows=0.

2 Comments

When I tried what I said, only the line with the description of excel is printed.( First row in excel)
In my opinion, the data frame seems to read 1 row because A, B, C of 1 row are merged. So it seems that there is no data when I skiprows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.