Home > Software engineering >  AttributeError: 'collections.OrderedDict' object has no attribute 'to_excel'
AttributeError: 'collections.OrderedDict' object has no attribute 'to_excel'

Time:05-06

My code is as follows:

import pandas as pd

df = pd.read_excel(r'C:\Users\user\Desktop\Test.xlsx',header=0,sheet_name=["Site 01","Site 08"],usecols="A:O")


df.to_excel(r'C:\Users\user\Desktop\Test-result.xlsx', index=False)

I get the error:

AttributeError: 'collections.OrderedDict' object has no attribute 'to_excel'

CodePudding user response:

If you set sheet_name with a list or None, you will get a dictionary of DataFrame. You have to use concat to merge them:

dfs = pd.read_excel('data.xlsx', sheet_name=['Site 01', 'Site 02'], usecols='A:B')
pd.concat(dfs).to_excel('output.xlsx', index=False)

Example:

>>> dfs
{'Site 01':    col1  col2
 0     1     2
 1     3     4,
 'Site 02':    col1  col2
 0     5     6}

>>> pd.concat(dfs)
           col1  col2
Site 01 0     1     2
        1     3     4
Site 02 0     5     6
  • Related