Miriya Molina
Miriya Molina

Reputation: 21

Why isn't index setting correctly when I convert this dataframe to dictionary?

If someone could please point me in the right direction I would be very grateful. I am attempting to convert a pandas dataframe into dictionary and use the index as the keys. The values in the rows would be the values of the dictionary, but I'm having trouble setting the index correctly. I need the dictionary format because it's part of something else that needs to be able to look up values efficiently.

#result from confidence intervals
conf_it = results.conf_int(alpha = 0.05)
conf_it = pd.DataFrame(conf_it)

#print output    
print(conf_it)

          0    1
panda     10   15
cat       7    9
dog       8    17


#rename and select relevant columns
conf_it = conf_it.rename(columns = {0: "lower", 1: "upper"})
LowPredictedImpact = conf_it[['lower']]

#convert dataframes into dictionaries
q = LowPredictedImpact.to_dict('index')

#Output
 list(q.items())

[('panda', {'lower': 10}),
 ('cat' , {'lower': 7}),
 ('dog', {'lower': 8})]

The output I need:

    [('panda', 10),
     ('cat' , 7),
     ('dog', 8)]

Other acceptable alternative output: 
 [({'panda': 10}),
     ({'cat': 7}),
     ({'dog': 8})]

If someone could please point me in the right direction I would be very grateful. 

Upvotes: 1

Views: 101

Answers (1)

Rajat Mishra
Rajat Mishra

Reputation: 3770

You dont need to convert your dataframe into a dictionary. This is can be achieved simply by creating a new column having the required values as tuple and then extract that column values into a list.

In [52]: df                                                                                                                                                                       
Out[52]: 
       lower  upper
key                
panda     10     15
dog        7      9
cat        8     17

In [53]: df.reset_index(inplace=True)                                                                                                                                             

In [54]: df['tuple'] = list(zip(df.key,df.lower))                                                                                                                                 

In [55]: df                                                                                                                                                                       
Out[55]: 
     key  lower  upper        tuple
0  panda     10     15  (panda, 10)
1    dog      7      9     (dog, 7)
2    cat      8     17     (cat, 8)

In [56]: final_output = df['tuple'].to_list()                                                                                                                                     

In [57]: final_output                                                                                                                                                             
Out[57]: [('panda', 10), ('dog', 7), ('cat', 8)]


Upvotes: 2

Related Questions