Back
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 value0 0 10.21 1 5.72 2 7.4
id value
0 0 10.2
1 1 5.7
2 2 7.4
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
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.
31k questions
32.8k answers
501 comments
693 users