如何使用map reduce创建Pandas数据帧?

2024-10-02 22:30:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看代码:

https://ahmedbesbes.com/how-to-mine-newsfeed-data-and-extract-interactive-insights-in-python.html

news = pd.DataFrame(reduce(lambda x,y: x+y ,map(lambda r: r['articles'], responses)))

有人能解释一下这句台词吗?map/reduce操作在这里做什么?在


Tags: andtolambda代码httpscommapreduce
1条回答
网友
1楼 · 发布于 2024-10-02 22:30:41

{cda>仅限函数。以及map,它将在列表中的每个元素中应用该函数。reduce将基于函数将列表设为单个值。在

这个操作用一个小例子来描述

In [2]: res
Out[2]: 
[{'articles': 124, 'other': 234},
 {'articles': 124, 'other': 234},
 {'articles': 124, 'other': 234}]

In [3]: map(lambda r: r['articles'], res)
Out[3]: [124, 124, 124]

In [4]: reduce(lambda x,y:x+y,[124, 124, 124])
Out[4]: 372

希望你能理解

相关问题 更多 >