1

i have a dictionary:

k_msc = {5.7:{50: 1 , 125: 0.5 ,200: 0.1}, 6.2:{50: 1 , 125: 0.5 ,200: 0.1}, 6.7:{50: 1 , 125: 0.5 ,200: 0.1}}

and a pandas data frame. like:

(     Std   pH  nacl  a2  a4  surfcov_msc  amount_mp  amount_a2  \
Run                                                                         
1     10  6.2   125      0.6       1.0         0.90      0.030       0.10   
2     21  6.2    50      1.0       0.7         0.90      0.005       0.25   
3     25  6.2   125      0.6       0.7         0.45      0.005       0.40   
4     20  5.7   125      1.0       0.7         0.00      0.030       0.10   
5     13  6.2   200      1.0       1.0         0.00      0.005       0.25   `

i do want to access the values stored in k_msc depending on the values in the columns pH and nacl:

def get_dict(mydict,mylist):
    a = list(zip( *[x for x in  mylist  ] ))
    list_items = [mydict[ x[0] ][ x[1] ] for x in a]
    return list_items

acces the values:

get_dict(k_msc, (df.pH, df.nacl))

This feels kind of odd. Is there an easier/clearer way ?

Moritz
  • 5,130
  • 10
  • 40
  • 81
  • somehow it feels like a bad idea to use floats as keys... – Pavel May 11 '14 at 19:16
  • yes, it does. Since the pH is a float by nature i do not know how to avoid that. I could multiply by 10. – Moritz May 11 '14 at 19:19
  • I tried the solution from the post Jan mentioned `getFromDict(k_msc, (df.pH, df.nacl))` but i do get the error: `TypeError: 'Series' objects are mutable, thus they cannot be hashed` – Moritz May 11 '14 at 19:21
  • I use python 3.3 so i do not have reduce(). – Moritz May 11 '14 at 19:23

0 Answers0