1

Edit: the duplicate suggested was not able to resolve my issue as the indexed column is different from turning a normal df into a dict. Would appreciate it if the downvoter takes away the vote.

Very simple, I want to create a dictionary from a df, with the df indexes as keys, and a column called 'signal' as key-values. I've used the to_dict() method, however this produces a nested dictionary, rather than individual. Code:

df:

index             signal
james.mccallum    0
john.driscoe      1
...               ...
andrew.black      0

input:
score_dict = df.to_dict()

produces:
score_dict = {signal{james.mccallum: 0, john.driscoe: 1, ... andrew.black: 0}

desired:
score_dict = {james.mccallum: 0, john.driscoe: 1, ... andrew.black: 0}

I'm sure it's a simple fix, however have not been able to find anything related to what I want to do. Any help would be appreciated.

Laurie
  • 1,189
  • 1
  • 12
  • 28
  • 2
    Possible duplicate of [Convert a Pandas DataFrame to a dictionary](https://stackoverflow.com/questions/26716616/convert-a-pandas-dataframe-to-a-dictionary) – scharette Jul 19 '18 at 13:50

1 Answers1

2

This is the intended behavior of to_dict (check this very good answer with inputs/outputs of the different possible args, followed with explanations on them).

In your case, just get all signal related values

>>> score_dict = df.to_dict()['signal']
{'james.mccallum': 0, 'john.driscoe': 1, 'andrew.black': 0}
rafaelc
  • 57,686
  • 15
  • 58
  • 82