stefanodv
stefanodv

Reputation: 533

how to read an excel file opened with pandas

I have to read some sheets in excel files that can be opened during the execution of the program. Furthermore I must have the possibility to modify them even if they are opened by python. I noticed that the command is not enough:

import pandas as pd
data = pd.read_excel(r"filename")

Should I use other packages?

Upvotes: 0

Views: 3958

Answers (3)

Rajnish kumar
Rajnish kumar

Reputation: 196

You are missing one parameter named sheet_name in pd.read_excel() mehtod.

You can do that like this:

import pandas as pd
df = pd.read_excel("filename.xlsx", sheet_name = "name_of_excel_sheet")

Hope it helps

Upvotes: 0

Sarang Narkhede
Sarang Narkhede

Reputation: 61

To read an excel file in pandas requires just couple of lines of code.

import pandas as pd
df = pd.read_excel("filename.xlsx", sep="separator")
df

Note : Separator attribute is required when you do not have a default separator (default is ',') in your input file. For example, if your input file is separated by ';' then you have to mention the separator attribute with value as ';'.

Upvotes: 0

Yong Wang
Yong Wang

Reputation: 1313

to write excel file via pandas DataFrame is quite convenient. You can just use read_excel or to_excel in batch mode. if you want to some advanced mode, for examples, write several sheets in one excel file. Pandas has already support it.

with pd.ExcelWriter('path_to_file.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Sheet1')
    df2.to_excel(writer, sheet_name='Sheet2')

If you want to to operate at the excel engine level, then you has to directly import those excel lib and use their functional directly. Here is some excel engine pandas supported. openpyxl: version 2.4 or higher is required xlsxwriter xlwt

Here is the pandas excel IO reference.

Upvotes: 2

Related Questions