dataframe to dict such that one column is the key and the other is the value [duplicate]

Something like this:

In [27]: df
Out[27]: 
  ID  A  B  C
0  p  1  3  2
1  q  4  3  2
2  r  4  0  9

In [30]: df.set_index('ID',inplace=True)
In [31]: df
Out[31]: 
    A  B  C
ID         
p   1  3  2
q   4  3  2
r   4  0  9

In [33]: df.to_dict()['B']
Out[33]: {'p': 3, 'q': 3, 'r': 0}

Leave a Comment