Search code examples
pythonexceldataframeordereddict

Python How to convert collections.OrderedDict to dataFrame


I have the following task: 1) I have an excel file with a few spreadsheets. From these spreadsheets I need information from columns "A:CU", rows 41 - 51 2) Then I need to collect information from from columns "A:CU", rows 41 - 51 from all spreadsheets (they have the same structure) and to create a database. 3) There should be a column that indicates from which spreadsheet data was collected

I did following:

import pandas as pd
file='January2020.xlsx'
#getting info from spreadsheets C(1), C(2) and so on
days = range(1,32)
sheets = []
for day in days:
    sheets.append('C(' + str(day)+')')
#importing data
all_sales=pd.read_excel(file,header=None,skiprows=41, usecols="A:CU", sheet_name=sheets,
                skipfooter=10)

Now I have collections.OrderedDict and struggle to put it into dataFrame.

enter image description here

What I need to have is a dataframe like this: enter image description here


Solution

  • I used this code and it worked:

    file='January2020.xlsx'
    days = range(1,32)
    all_sales=pd.DataFrame()
    df = pd.DataFrame()
    all_df = []
    for day in days:
        sheet_name = "C("+str(day)+")"
        all_sales=pd.read_excel(file,header=None,skiprows=41,usecols="A:CU", sheet_name=sheet_name,
                    skipfooter=10)
        all_sales["Date"] = sheet_name
        all_df.append(all_sales)
    df_final = pd.concat(all_df)