简体   繁体   中英

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.

在此处输入图片说明

What I need to have is a dataframe like this: 在此处输入图片说明

试试pd.concat

df = pd.concat(all_sales, ignore_index = True) 

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM