简体   繁体   中英

Exporting the List of columns and list for values for that columns to Excel

I have a list of ID's which i'm looping through and i'm getting the List of FIELDS and VALUES.

Now i have to put both the list into Excel file where FIELDS are the Columns and VALUES are the rows for that columns as i said there is List of ID's its means every id have different VALUES but the Fields are same. and VALUES should have the list/dict or anything what ever we find in value list we will put it into excel.

for that i have tried with these code:

field_nameList = []
field_valueList = []

for id in IDS:
    for field in id:
        field_value = id[field]

        ..... some code........

        field_name = id[field]

        ..... some code........

        field_nameList.append(field_name)
        field_valueList.append(field_value)

# Here after getting all the ID's Fields and Value i'm converting it to dictionary to export it to excel using pandas.

fieldsItems = dict(zip(field_nameList, field_valueList)) 
df = pd.DataFrame(fieldsItems)
df.to_excel('GXfile.xlsx', index=None,header=True)

For single ID its worked but when i have multiple ID's and then i'm trying to do the same i'm getting

Error:Mixing dicts with non-Series may lead to ambiguous ordering.

My main motive is for every id whatever list i'm getting I need to create a columns and from next loops onwards just keep adding the row for the FIELDS.

I know maybe my code implementation is not correct but please suggest me a better way.

Added Fieldname which is columns name here and the values this is the sample excel data

Id  Resolution  subStatus           Last Viewed                     Σ Original Estimate Issue Type  CCB Decision    Reason For Escalation   CCB Comments    Issue Service Affecting?    Estimated Effort to Fix Reproducibility Problem Description/Systems Impacted    Symptoms    Release Note Status Release CCB Estimated Effort to Verify Fix
1   None        Screening Required  2020-01-30T06:38:45.668+0000    144000           Bug        Fix     Defer           ccb testing Yes             test                                        Never       test            test        Unspecified 
2   None        Screening Required  2020-01-30T06:38:45.668+0000    144000           Bug        Fix     Defer           ccb testing Yes             test                                        Never       test            test        Unspecified 

@The Guy i'm sorry for not explaining properly i appreciate the help and code but there one misunderstanding here i have 3 list first list is for ID's in that other 2 list which i'm getting dynamically from code so let me write again:

First list :

ID = [1,2,3,4..]

2nd List:

Field_Name =['A','B','C',...so on] this will be achieved by code 

3rd List:

Field_value =['A_values','B_values','C_values,....so on]

Excel File should contain:

ID  A         B         C          ....
1   A_values  B_values  C_values   .....

Here we can clearly see that i'm getting the Fields_name as a columns and Filed_value as a row for that particular columns ...how we can get this.

According to your updated post:

import pandas as pd

ids=[1,2,3]
Field_Name =['A','B','C']
Field_value =['A_values','B_values','C_values']

zipped_name_and_value=dict(zip(Field_Name,Field_value))

df=pd.DataFrame([zipped_name_and_value])
df.index+=1

df.index.names=['Id']

Output

       A          B           C
Id          
1   A_values    B_values    C_values

In above case Id is not coming from list.

If you want all three lists ids, Field_Name, FieldValue should be used, in that case following is the code:

import pandas as pd

ids=[1,2,3] 
Field_Name =['A','B','C']
Field_value =[['A_values','B_values','C_values'],['A_values','B_values','C_values'],['A_values','B_values','C_values']]


df=pd.DataFrame(Field_value,index=ids,columns=Field_Name)

Output

         A          B         C
Id          
1   A_values    B_values    C_values
2   A_values    B_values    C_values
3   A_values    B_values    C_values

Note : You have to make sure that the Field_Value has nested list, because all the nested list will be treated as row.

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