【发布时间】:2016-05-02 11:47:07
【问题描述】:
熊猫系列是否有reduce 的类似物?
例如,map 的模拟是 pd.Series.apply,但我找不到 reduce 的任何模拟。
我的应用是,我有一个 pandas 系列的列表:
>>> business["categories"].head()
0 ['Doctors', 'Health & Medical']
1 ['Nightlife']
2 ['Active Life', 'Mini Golf', 'Golf']
3 ['Shopping', 'Home Services', 'Internet Servic...
4 ['Bars', 'American (New)', 'Nightlife', 'Loung...
Name: categories, dtype: object
我想使用reduce 将一系列列表合并在一起,如下所示:
categories = reduce(lambda l1, l2: l1 + l2, categories)
但这需要很长时间,因为在 Python 中将两个列表合并在一起是 O(n) 时间。我希望pd.Series 有一种矢量化的方式来更快地执行此操作。
【问题讨论】:
标签: python performance pandas vectorization reduce