4

I have the foll. csv with 1st row as header:

A      B
test    23
try     34

I want to read in this as a dictionary, so doing this:

dt = pandas.read_csv('file.csv').to_dict()

However, this reads in the header row as key. I want the values in column 'A' to be the keys. How do I do that i.e. get answer like this:

{'test':'23', 'try':'34'}
user308827
  • 21,227
  • 87
  • 254
  • 417
  • 1
    So `{'A': ['test', 'try'], 'B': ['23', '34']}`? – Zizouz212 Nov 10 '15 at 00:43
  • Thanks for clarifying this, I want {'test':'23', 'try':'34'}. Updated question – user308827 Nov 10 '15 at 00:45
  • And your delimiter is `\t`? Generally, `csv` files are `1, 2, 3\n4, 5, 6`. Commas and newlines to separate columns and rows. – Zizouz212 Nov 10 '15 at 00:46
  • Possible duplicate of [Convert Pandas DataFrame to dictionary](http://stackoverflow.com/questions/26716616/convert-pandas-dataframe-to-dictionary) – R Nar Nov 10 '15 at 00:47
  • @RNar Nah, they look like completely different questions with different objects. – Zizouz212 Nov 10 '15 at 00:49
  • @Zizouz212 i will have to disagree with you. the inherent problem is that he wants to have a dict where the keys are the values in a column rather than the columns themselves. the answer to that is to set that column is the index of the dataframe then call `to_dict('list')` – R Nar Nov 10 '15 at 00:53
  • @RNar Ah, but thats calling from a `pd.DataFrame` object, while this is from a `.csv` file, which are vastly different situations. – Zizouz212 Nov 10 '15 at 00:54
  • @Zizouz212 a `.csv` file which he is parsing as a pd.DataFrame with `pandas.read_csv` – R Nar Nov 10 '15 at 00:56
  • @RNar Exactly, the questions are different, and therefore, the answers will be different as well. They need to be nearly identical to be considered duplicates. – Zizouz212 Nov 10 '15 at 00:58

2 Answers2

14
dt = pandas.read_csv('file.csv', index_col=1, skiprows=1).T.to_dict()
Alexander
  • 105,104
  • 32
  • 201
  • 196
5

Duplicating data:

import pandas as pd
from io import StringIO

data="""
A      B
test    23
try     34
"""

df = pd.read_csv(StringIO(data), delimiter='\s+')

Converting to dictioanry:

print(dict(df.values))

Will give:

{'try': 34, 'test': 23}
Leb
  • 15,483
  • 10
  • 56
  • 75