2

I have a excel sheet i.e,

   0 first     |second    |third      |root
   1 credible  |credulous |credibility|cred
   2 cryptogram|cryptology|cryptic    |crypt
   3 aquatic   |aquarium  |aqueduct   |aqua


i want to import key and value for dictionary from above mentioned excel sheet:
for example: i have written a dictionary code for storing this values

   new_dict = {
    ('credible','credulous','credibility') : 'cred',
    ('cryptogram','cryptology','cryptic')   : 'crypt'
    }

so instead of writing key and value for each word in dictionary i want to import from excel where first 3 column(i.e first,second,third) should be the key and last column(i.e root) will be the value of a dictionary. Is it possible to do that?
sorry for my English.
Thanks

Rohan
  • 41
  • 7

2 Answers2

1

Use set_index to set the index for every column (first three, in this case) besides the root, and then call root.to_dict:

df.set_index(df.columns.difference(['root']).tolist()).root.to_dict() 
{
    ('aquatic', 'aquarium', 'aqueduct')      : 'aqua',
    ('credible', 'credulous', 'credibility') : 'cred',
    ('cryptogram', 'cryptology', 'cryptic')  : 'crypt'
}
cs95
  • 379,657
  • 97
  • 704
  • 746
1

Use set_index by first 3 columns with Series.to_dict:

d = df.set_index(['first','second','third'])['root'].to_dict()
print (d)
{('credible', 'credulous', 'credibility'): 'cred',
 ('aquatic', 'aquarium', 'aqueduct'): 'aqua', 
 ('cryptogram', 'cryptology', 'cryptic'): 'crypt'}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252