[英]Python How to convert collections.OrderedDict to dataFrame
I have the following task: 1) I have an excel file with a few spreadsheets.我有以下任务:1)我有一个包含几个电子表格的 excel 文件。 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.从这些电子表格中,我需要来自“A:CU”列、第 41 - 51 行的信息 2) 然后我需要从所有电子表格的“A:CU”列、第 41 - 51 行中收集信息(它们具有相同的结构)和创建一个数据库。 3) There should be a column that indicates from which spreadsheet data was collected 3) 应该有一列指示从哪个电子表格数据收集
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.现在我有 collections.OrderedDict 并努力将它放入 dataFrame。
试试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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.