从Pandas df创建一个字典列表

4

I have a pandas dataframe of the the form

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

我希望把前两列转换成字典列表,即 [{'x': 0.5,'y': 0.1},{'x': 0.6,'y': 0.2} ...]。
我可以写一个循环来完成这个愚蠢的方法,但有没有更好更快的方法呢?
3个回答

3
您可以使用iterrows来实现。这使您可以像使用iteritems()__getitem__等方法一样遍历行,但是返回的是Series而不是字典。
如果您必须使用字典,您可以使用to_dict()方法将每个Series转换为字典。
例如:
list_of_dicts = list( row.to_dict() for key, row in df.iterrows() )

@ganeshreddy,好的。我已经添加了它。 - shx2
这也给我一个错误:元组对象没有to_dict属性。 - ganesh reddy
@ganeshreddy,啊,已经修复了。 - shx2

2

使用to_dict(orient='records')并指定orient=records可以提高速度。

In [2]: df[['x', 'y']].to_dict(orient='records')
Out[2]:
[{'x': 0.5, 'y': 0.1}, {'x': 0.6, 'y': 0.2}]

时间


In [8]: df.shape
Out[8]: (10000, 4)

In [9]: %timeit df[['x', 'y']].to_dict(orient='records')
10 loops, best of 3: 68.4 ms per loop

In [10]: %timeit df[['x','y']].to_dict('index').values()
1 loop, best of 3: 570 ms per loop 

In [11]: %timeit list(row.to_dict() for key, row in df[['x', 'y']].iterrows())
1 loop, best of 3: 575 ms per loop

0
你可以使用to_dict()方法。假设yourdata.csv是以.csv格式存储的数据:
df = pd.read_csv('yourdata.csv')

d = df[['x','y']].to_dict('index').values()

应该可以工作。它返回:

[{'y': 0.1, 'x': 0.5}, {'y': 0.2, 'x': 0.6}]

@shx2 我认为是这样的,因为 'index' 参数作为 {index -> {column -> value} 运作。 - Fabio Lamanna
对我来说它有效,我刚刚使用 pd.read_csv('yourdata.csv') 导入了你的数据框。 - Fabio Lamanna
valuError: outtype索引未被理解。 - ganesh reddy
@ganeshreddy 你使用的 pandas 版本是哪个?正如我所说,我无法重现你的错误。 - Fabio Lamanna
0.13.1 是该版本。 - ganesh reddy
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接