2

I'm looking to do a row by row dictionary mapping of two columns of a headered multi-column csv read in to a pandas DataFrame via read_csv(). In other words, for each row, make the value in column X the key, and the corresponding value in column Y the value.

With many operations in pandas being element-wise, I surmised that I could possibly achieve this with the following one-liner:

{df['X'] : df['Y']}

Alas, no such luck.

Does pandas expose its own way of doing this? I'll also be looking to something similar with a .xls and a .xlsx.

  • Python: 2.7.11

  • Pandas: 0.18

Charlie Clark
  • 18,477
  • 4
  • 49
  • 55
Pyderman
  • 14,809
  • 13
  • 61
  • 106
  • 1
    Possible duplicate of http://stackoverflow.com/questions/17426292/what-is-the-most-efficient-way-to-create-a-dictionary-of-two-pandas-dataframe-co ? – Pyderman May 13 '16 at 01:25

2 Answers2

1

How about:

dict(zip(df['X'].values, df['Y'].values))
JRach
  • 11
  • 2
0

If all of the values in X are not unique, then you will be overwriting your keys.

Assuming that they are unique, then you can use a dictionary comprehension with zip.

{key: val for key, val in zip(df.X, df.Y)}
Alexander
  • 105,104
  • 32
  • 201
  • 196