简体   繁体   English

Python 如何将 collections.OrderedDict 转换为 dataFrame

[英]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。

在此处输入图片说明

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - collections.OrderedDict() 未正确排序字典 - Python - collections.OrderedDict() is not ordering dictionary properly 如何通过python 3.5输入模块键入提示collections.OrderedDict - how to type hint collections.OrderedDict via python 3.5 typing module Python 3.7+ 规范是否保证 `collections.OrderedDict 不是 dict`? - Does the Python 3.7+ spec guarantee `collections.OrderedDict is not dict`? Django:'collections.OrderedDict' object 不可调用 - Django : 'collections.OrderedDict' object is not callable “collections.OrderedDict”对象没有属性 - 'collections.OrderedDict' object has no attribute collections.OrderedDict 不适用于 json.dump() - collections.OrderedDict not working on json.dump() 使用collections.OrderedDict是不好的做法吗? - Is it bad practice to use collections.OrderedDict? 通过索引访问 collections.OrderedDict 中的项目 - Accessing items in an collections.OrderedDict by index (1)TypeError: 不能连接类型为'的object<class 'collections.ordereddict'> '; 只有系列和 DataFrame 对象有效 (2)</class> - (1)TypeError: cannot concatenate object of type '<class 'collections.OrderedDict'>'; only Series and DataFrame objs are valid (2) Django AttributeError: &#39;collections.OrderedDict&#39; 对象没有属性 &#39;pk&#39; - Django AttributeError: 'collections.OrderedDict' object has no attribute 'pk'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM