-1

I have some data in an excel sheet shown in picture below that I want to read as dataframe using pandas.read_excel, however the function skips automatically the first 2 rows of the sheet as shown in image below. I can't figure out the problem source? and how can I solve it?

enter image description here

the following code is used:

import pandas as pd
import tkinter as Tk
from tkinter.filedialog import askopenfilename
from tabulate import tabulate

print("Choose the desired Source file:")
Tk.Tk().withdraw()
path1 = askopenfilename() # the path to the file I want to use

source_workbook = pd.read_excel(path1, header=1, engine='openpyxl')
print(tabulate(source_workbook,headers='firstrow'))

The above code produces this result:

enter image description here

When I change the position of the excel table as shown below, the function produces the desired data frame shown in the image below also.

enter image description here

enter image description here

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Farouk
  • 21
  • 1
  • 7
  • try header=0 while reading pd.read_excel() – darth baba Jun 23 '23 at 17:03
  • header=0 by default gives the df column names. – Hermann12 Jun 23 '23 at 17:06
  • 1
    [Please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341). Copy the text itself and use the formatting tools like [code formatting](/editing-help#code). And please provide a [mre]. Tkinter doesn't seem relevant, so try removing it from the equation. And is `tabulate` relevant? Try just `print(df)` and see if the same issue occurs. Also if it helps: [How to make good reproducible pandas examples](/q/20109391/4518341) – wjandrea Jun 23 '23 at 17:13
  • I tried header=0 and I also removed the header, the same problem persists it started from the first row of data and took it as the column names for the data frame. – Farouk Jun 23 '23 at 17:15
  • I was able to reproduce the issue. Remove `header=1`; it'll do `header=0` by default, which is what you want. Secondly, `headers='firstrow'` should be `headers='keys'`, or you could just `print(df)` instead. – wjandrea Jun 23 '23 at 17:20
  • Thank you very much, the problem is solved. – Farouk Jun 23 '23 at 17:32

1 Answers1

1

Thanks to wjandrea for the answer:

Remove header=1; it'll do header=0 by default, which is what you want. Secondly, headers='firstrow' should be headers='keys', or you could just print(df) instead.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Farouk
  • 21
  • 1
  • 7