0

I have a list like this:

gene1 0.9018  0.6512  0.0368  0.0 ...
gene2 0.0     0.0     0.0     0.0 ...
gene3 0.0521  0.0     0.773   0.0 ...  
...

I want the first column (gene) to be the key, and the next a columns to be the values. How do I do this in pandas?

  • Possible duplicate of [python pandas dataframe to dictionary](http://stackoverflow.com/questions/18695605/python-pandas-dataframe-to-dictionary) – dabadaba Mar 10 '17 at 21:39

1 Answers1

0

You can investigate .to_dict() for this, but that can require wrangling with nesting. As an alternative, you might find it easier to zip the columns:

new_dict = {x: y for x,y in zip(df['gene'], df['column_2'])}

UPDATE

Actually, zipping is silly: the data is already paired up! Instead, just go to the underlying numpy ndarray for the appropriate slice:

new_dict = {x: y for x,y in df.loc[:, ['gene', 'column_2']].values}
pml
  • 484
  • 3
  • 12
  • thanks, but I don't have column headers. I want to specify the first column [0] as the key and everything beyond [1:] as values – alexroads Mar 10 '17 at 22:12
  • Well, now I'm confused. Is this data even in pandas? If you just have a tab-separated file that you want in dicts (not a dataframe) you'd be better off reading it line by line into a dict... – pml Mar 10 '17 at 23:29