gpt4 book ai didi

dictionary - 从 Pandas df 创建字典列表

转载 作者:行者123 更新时间:2023-12-04 01:34:38 24 4
gpt4 key购买 nike

我有一个表格的 Pandas 数据框

0   x     y    z
1 .5 .1 4
2 .6 .2 5

我想将其转换为前两个列的字典列表,即
[{'x':0.5, 'y':0.1}, {'x':0.6, 'y':0.2}.....]

我可以写一个循环并用愚蠢的方式来做,有没有更好更快的方法?

最佳答案

您可以使用 iterrows为了那个原因。这使您可以将行迭代为 Series ,不是 dicts,但非常相似(例如有 iteritems()__getitem__ 等)。

如果您必须使用字典,您可以轻松地转换每个 Series听写,使用 to_dict()方法。

例如:

list_of_dicts = list( row.to_dict() for key, row in df.iterrows() )

关于dictionary - 从 Pandas df 创建字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com