Here is how:
d = {k:df['b'][df[df['a']==k].index.values[0]] for k in df['a']}
Breaking it down, here is how to find the index of a specific value in column:
val = 'value' # The value we want to find the index of
col = 'a' # The column the value is in
index = df[df[col]==val].index.values[0] # Here is how to find the index
Here is how you can find the value of a specific column given an index:
index = 3 The index of the column we want to find the value of
col = 'a' # The column which want to find a value in
value = df[col][index] # Here is how to find the value
So the dictionary will basically have the values of column a
, and the values of column b
, which we find those values that correspond to the values from a
by using the indexes.