简体   繁体   English

python数据框自动生成excel表

[英]python dataframe auto-generate excel sheet

I have a massive data-set that is entirely repetitive and i was thinking the best way to automate the task was with python.我有一个完全重复的海量数据集,我认为自动化任务的最佳方法是使用 python。 This is for an engineering project that uses motors of only 2 types, Follower, or Leader.这是针对仅使用 2 种类型电机的工程项目,Follower 或 Leader。

For Follower it will have a structure exactly like this(where IFDXXXX is will be the individual drive number):对于Follower,它的结构将与此完全相同(其中IFDXXXX 将是单个驱动器编号):

在此处输入图片说明

For a Leader it will have a structure exactly like this(where IFDXXXX is will be the individual drive number):对于Leader,它将具有与此完全相同的结构(其中 IFDXXXX 将是单个驱动器编号):

在此处输入图片说明

My idea is that im going to import an excel sheet with the following format and store it as a dataframe with pandas to manipulate later for auto-generation:我的想法是,我将使用以下格式导入一个 Excel 工作表,并将其存储为带有 Pandas 的数据框,以便稍后进行自动生成:

在此处输入图片说明

Whats the easiest approach to do this, or is there another easier method?什么是最简单的方法,或者有另一种更简单的方法吗? I would like to stick with python if possible as im trying to expand my knowledge with this language.如果可能的话,我想坚持使用 python,因为我试图用这种语言扩展我的知识。

EDIT:编辑:

The ultimate end goal is to end up with a sheet that looks something like this:最终的最终目标是得到一张看起来像这样的表:

在此处输入图片说明

Update1:更新1:

在此处输入图片说明

suppose you have a table like you wanted to save with headlines drive_id and role and they are saved in dataframe df1 to iterate over your lines and create a new table, I prefer doing it with a list of dicts假设你有一个像你想用标题drive_idrole保存的表,它们保存在数据帧 df1 中以迭代你的行并创建一个新表,我更喜欢用一个字典列表来做

out_table = []
for index, row in df1.iterrows()
    drive_id = row["drive id"]
    if row["role"]=="follower":
        out_row ={}
        out_row["drive_id"]= drive_id
        out_row["task name"] = "rotate rev"
        out_row["description"] = "check rotate hmi"
        out_row["comment"] = "NA"
        out_table.append(out_row)
#this is the end of the first row, so on you add all the rows for this role
        out_row ={}
        out_row["drive_id"] =  driver_id
        out_row["task name"] = "rotate fwd"
        out_row["description"] = "check hdmi for footlock"
        out_row["comment"] = ""
        out_table.append(out_row)
    if row["role"] == "FOLLOWER"
# here you add all the rows for this role


df2 = pd.DataFrame(out_table)  # this makes a dataframe where the dict keys are the table headers
df2.to_excel(r"D:\drive_table.xlsx")
import pandas as pd

df1=pd.read_excel('insert file path here',sheet_name = 0)

this allows pandas to store the excel sheet as a dataframe.这允许 Pandas 将 Excel 表存储为数据框。

if you want to push a dataframe that is produced after your code you can use如果您想推送在您的代码之后生成的数据帧,您可以使用

df.to_excel(x)

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

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