简体   繁体   中英

create a dict of dict from a dataframe

i am trying to convert a data-frame to a dict in the below format:

df
    name   age country state  pincode
0  user1  10.0      in    tn      1.0
1  user2  11.0      us    tx      2.0
2  user3  12.0      eu    fr      3.0
{
   'user1':{'age':10,'country':'in','state':'tn','pincode':1},
   'user2':{'age':11,'country':'us','state':'tx','pincode':2},
   'user3':{'age':12,'country':'eu','state':'fr','pincode':3}
}

I am currently doing this by below statement:

op = {}
for i, row in df.iterrows():
    op[row['name']] = {'age':row['age'],'country':row['country'],'state':row['state'],'pincode':row['pincode']}

I want a the solution to work if there are additional columns added to the df. for example telephone number. Since the statement I have written is static it won't give me the additional rows in my output. Is there a built in method in pandas that does this?

You want to set name as index first:

df.set_index('name').to_dict('index')

Output:

{'user1': {'age': 10.0, 'country': 'in', 'state': 'tn', 'pincode': 1.0},
 'user2': {'age': 11.0, 'country': 'us', 'state': 'tx', 'pincode': 2.0},
 'user3': {'age': 12.0, 'country': 'eu', 'state': 'fr', 'pincode': 3.0}}

Use DataFrame.set_index with DataFrame.to_dict with orient='index' :

d = df.set_index('name').to_dict(orient='index')
print (d)
{'user1': {'age': 10.0, 'country': 'in', 'state': 'tn', 'pincode': 1.0}, 
 'user2': {'age': 11.0, 'country': 'us', 'state': 'tx', 'pincode': 2.0}, 
 'user3': {'age': 12.0, 'country': 'eu', 'state': 'fr', 'pincode': 3.0}}

If possible another columns which should be omited is possible crop them before or selecting:

d = df.set_index('name')[['age','country','state','pincode']].to_dict(orient='index')

This may not be very readable, but here's a one line dictionary comprehension:

{k:{a:b for a,b in zip(df.columns.tolist()[1:], v)}
 for k,v in zip(df['name'].to_list(), df.iloc[:,1:].to_numpy().tolist())}

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