Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I've a two columns dataframe, and intend to convert it to python dictionary - the first column will be the key and the second will be the value. Thank you in advance.

Dataframe:

    id    value

0    0     10.2

1    1      5.7

2    2      7.4

1 Answer

0 votes
by (41.4k points)

  Try to use Zip

df = pd.read_csv("file")

d= dict([(i,[a,b,c ]) for i, a,b,c in zip(df.ID, df.A,df.B,df.C)])

print d

Output:

{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

 

 

Related questions

Browse Categories

...