Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Convert a DataFrame of 2 columns into a dict ColA -> ColB

$
0
0

If I had a DataFrame of 2 columns(a and b)How do I convert this into a dictionary mapping a->b?(None of the DataFrame.to_dict() options is doing what I want

pd.DataFrame(data={ 'a':[1,3], 'b':[2,4]})   a  b0  1  21  3  4

and I want a resulting dictionary like

{ 1:2, 3:4 }

This works but there must be a better way?

>>> df = pd.DataFrame(data={ 'a':[1,3], 'b':[2,4]})>>> { r[1][0] : r[1][1] for r in df.iterrows() }{1: 2, 3: 4}

Viewing all articles
Browse latest Browse all 11661

Trending Articles