Convert a Pandas DataFrame to a dictionary

前端 未结 7 1030
悲哀的现实
悲哀的现实 2020-11-22 08:07

I have a DataFrame with four columns. I want to convert this DataFrame to a python dictionary. I want the elements of first column be keys and the elements of o

7条回答
  •  星月不相逢
    2020-11-22 08:29

    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]}
    

提交回复
热议问题