简体   繁体   中英

Multiple excel data storing in Dictionary using Python

I am stuck in one problem and am not able to go ahead.. please need help to move further. I have input excel in this format...

在此处输入图片说明

Name    usn          Sub   marks
dhdn    1bm15mca13    c     90
                     java   95
                     python 98
subbu   1bm15mca13   java   92
                     perl   91
paddu   1bm15mca13    c#    80
                     java   81

And am trying to get expected dictionary in this format:

d = [
{
"name":"dhdn",
"usn":1bm15mca13",
"sub":["c","java","python"],
"marks":[90,95,98]
},
{
"name":"subbu",
"usn":1bm15mca14",
"sub":["java","perl"],
"marks":[92,91]
},
{
"name":"paddu",
"usn":1bm15mca17",
"sub":["c#","java"],
"marks":[80,81]
}
]

Tried code but it is working for only two column

import pandas as pd
existing_excel_file = 'test.xls'

df_service = pd.read_excel(existing_excel_file, sheet_name='Sheet1')

df_service = df_service.fillna(method='ffill')
result = [{'name':k,'sub':g["sub"].tolist()} for k,g in df_service.groupby("name")]
print (result)

Please provide idea or suggestion to solve my problem.

import pandas as pd
existing_excel_file = 'test.xls'

df_service = pd.read_excel(existing_excel_file, sheet_name='Sheet1')

df_service = df_service.fillna(method='ffill')
result = [{'name':k[0],'usn':k[1],'sub':v["sub"].tolist(),"marks":v["marks"].tolist()} for k,v in df_service.groupby(['name', 'usn'])]
pprint (result)

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