+4 votes
in Programming Languages by (77.7k points)
I want to read data from an excel file into a pandas dataframe. How to read an Xls file using pandas?

1 Answer

+1 vote
by (355k points)
selected by
 
Best answer

You can use pandas' read_excel() function to read data from an excel file. The function has many parameters, and their default values work most of the time. However, parameters io, sheet_name, and header should be provided by the user.

io: the name of the excel file

sheet_name: if there are multiple tabs in the input excel file, you should mention the tab you want to read. e.g., "Sheet1": load sheet with the name "Sheet1".

header: row number that you want to use for the column labels of the parsed DataFrame.

Here is an example to read data from an excel file using pandas:

import pandas as pd

# read data from an excel file
df = pd.read_excel("testfile.xlsx", sheet_name="Sheet1", header=0)
print(df)

The above code prints the data read from the excel file "testfile.xlsx" into a dataframe:

Name   Age
0   AAA   23
1   BBB   34
2   CCC   56
3   DDD   45
4   EEE   67


...