简体   繁体   中英

Python: Copying certain columns to an empty dataframe using For loop

wo = "C:/temp/temp/WO.xlsx"
dfwo = pd.read_excel(wo)

columnnames = ["TicketID","CreateDate","Status","Summary","CreatedBy","Company"]
main = pd.DataFrame(columns = columnnames)

for i in range(0,15):
    print(i)
    main["TicketID"][i] = dfwo["WO ID"][i]
    main["CreateDate"][i] = dfwo["WO Create TimeStamp"][i]
    main["Status"][i] = dfwo["Status"][i]
    main["Summary"][i] = dfwo["WO Summary"][i]
    main["CreatedBy"][i] = dfwo["Submitter Full Name"][i]
    main["Company"][i] = dfwo["Company"][i]

I am trying to copy selected columns from 1 df to another. dfwo is a df derived from Excel Main is an empty dataframe and has selected columns from dfwo

When I run this code, it gives me the error, "IndexError: index 0 is out of bounds for axis 0 with size 0"

Any suggestions pls?

wo = "C:/temp/temp/WO.xlsx"
dfwo = pd.read_excel(wo)

columnnames =["TicketID","CreateDate","Status","Summary","CreatedBy","Company"]
main = dfwo[columnnames]
new_col_names =  {
    "TicketID":"WO ID",
    "CreateDate":"WO Create TimeStamp",
    "Status":"Status",
    "Summary":"WO Summary",
    "CreatedBy":"Submitter Full Name",
    "Company":"Company"
}
main.rename(columns = new_col_names,inplace = True)

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