在pandas Series中有没有类似于reduce
的模拟函数?
例如,map
的仿函数是pd.Series.apply,但我找不到任何reduce
的仿函数。
我的应用场景是,我有一个包含列表的pandas Series:
>>> 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
将不同列表的Series合并在一起,就像这样:
categories = reduce(lambda l1, l2: l1 + l2, categories)
但是这需要很长时间,因为在Python中合并两个列表需要 O(n)
的时间。我希望pd.Series
有一种矢量化的方法可以更快地执行此操作。
chain
可以显著减少内存分配的数量。 - Mike Müller