Convert a Pandas DataFrame to a dictionary

前端 未结 7 972
悲哀的现实
悲哀的现实 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:23

    If you don't mind the dictionary values being tuples, you can use itertuples:

    >>> {x[0]: x[1:] for x in df.itertuples(index=False)}
    {'p': (1, 3, 2), 'q': (4, 3, 2), 'r': (4, 0, 9)}
    

提交回复
热议问题