I have the following Python code:
JavaScript
x
12
12
1
import pandas as pd
2
path=r"C:UsersWaliExample.xls"
3
df1=pd.read_excel(path, sheet_name = [0])
4
df2=pd.read_excel(path, sheet_name = [1])
5
6
with pd.ExcelWriter(r"C:UsersWaliExample2.xls") as writer:
7
8
# use to_excel function and specify the sheet_name and index
9
# to store the dataframe in specified sheet
10
df1.to_excel(writer, sheet_name="1", index=0)
11
df2.to_excel(writer, sheet_name="2", index=1)
12
I’m reading the excel file which contains two sheets and then saving those sheets into a new excel file but unfortunately I’m receiving the following error:
JavaScript
1
2
1
AttributeError: 'dict' object has no attribute 'to_excel'
2
Any ideas on how I can fix this?. Thanks.
Advertisement
Answer
Change [0] to 0 in pd.read_excel(path, sheet_name = [0]) will resolve this issue
JavaScript
1
12
12
1
import pandas as pd
2
path=r"test_book.xlsx"
3
df1=pd.read_excel(path, sheet_name = 0)
4
df2=pd.read_excel(path, sheet_name = 1)
5
6
with pd.ExcelWriter(r"test_book1.xlsx") as writer:
7
8
# use to_excel function and specify the sheet_name and index
9
# to store the dataframe in specified sheet
10
df1.to_excel(writer, sheet_name="1", index=0)
11
df2.to_excel(writer, sheet_name="2", index=1)
12