简体   繁体   中英

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. This is for an engineering project that uses motors of only 2 types, Follower, or Leader.

For Follower it will have a structure exactly like this(where IFDXXXX is will be the individual drive number):

在此处输入图片说明

For a Leader it will have a structure exactly like this(where IFDXXXX is will be the individual drive number):

在此处输入图片说明

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:

在此处输入图片说明

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.

EDIT:

The ultimate end goal is to end up with a sheet that looks something like this:

在此处输入图片说明

Update1:

在此处输入图片说明

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

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.

if you want to push a dataframe that is produced after your code you can use

df.to_excel(x)

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