0

Edit: I found my answer here python pandas dataframe columns convert to dict key and value

Original post: I am attempting to write a specific dataframe column to a dictionary, yet I am also writing the index at the same time which I do not want. How do I do this the right way so I don't also include the index?

vi_dict = {}

vi_table_rvi = VI_table['RVI'].unique()

for d in vi_table_rvi:
    vi_dict[f'{d}'] = VI_table['AMT'].loc[(VI_table['RVI'] == d) & (VI_table['CD'] == 102)]

My dictionary output reads something like:

key: index value

rather than

key: value

Thanks for any help.

mattie_g
  • 95
  • 9

1 Answers1

0

You are storing a pandas series in that case, in which you have the index. If you want to store the values only, use the to_numpyfunction. It will store a numpy array with the values of the series.

vi_dict[f'{d}'] = VI_table['AMT'].loc[(VI_table['RVI'] == d) & (VI_table['CD'] == 102)].to_numpy()
Thombou
  • 367
  • 2
  • 15
  • Thanks for the teaching. I didn't realize this was a series until investigating with type(). I did find the answer I was looking for in this post though, which involves the dict() and zip() methods https://stackoverflow.com/questions/18012505/python-pandas-dataframe-columns-convert-to-dict-key-and-value – mattie_g Jan 06 '20 at 16:45
  • Perfect. Then I suggest that you edit your question to mention that you found the answer. – Thombou Jan 06 '20 at 17:15