python数据框架自动生成excel工作表

2024-10-03 23:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个完全重复的海量数据集,我认为自动化任务的最佳方法是使用python。这适用于仅使用两种类型的电机的工程项目,即从动电机或先导电机

对于从动件,其结构与此完全相同(其中IFDXXXX为单个驱动器编号):

enter image description here

对于引线,其结构与此完全相同(其中IFDXXXX将是单个驱动器编号):

enter image description here

我的想法是,我将导入以下格式的excel工作表,并将其存储为带有pandas的数据框,以便以后自动生成:

enter image description here

做这件事最简单的方法是什么,或者还有其他更简单的方法吗?如果可能的话,我想继续使用python,因为我试图用python扩展我的知识

编辑:

最终目标是最终得到一张类似以下内容的工作表:

enter image description here

更新1:

enter image description here


Tags: 数据方法类型pandas格式结构excel工程项目
2条回答

假设您有一个表,您想保存标题驱动器id角色 它们保存在数据帧df1中 要遍历您的行并创建一个新表,我更喜欢使用dict列表

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)

这允许pandas将excel工作表存储为数据框

如果要推送在代码之后生成的数据帧,可以使用

df.to_excel(x)

相关问题 更多 >