【问题标题】:How is map reduce being used to create pandas data frame?如何使用 map reduce 来创建 pandas 数据框?
【发布时间】:2017-12-27 11:59:06
【问题描述】:

我正在查看此页面上的代码:

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 操作在这里做什么?

【问题讨论】:

    标签: python pandas lambda


    【解决方案1】:

    lambda 这只是一个函数。 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
    

    希望你明白这一点,

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多