我有以下系列:
s = pd.Series([['a', 'b'], ['c', 'd'], ['f', 'g']])
>>> s
0 [a, b]
1 [c, d]
2 [f, g]
dtype: object
什么是串联该系列中所有列表的最简单方法(最好是矢量化方法),所以我得到:
l = ['a', 'b', 'c', 'd', 'f', 'g']
谢谢!
解决方法:
嵌套列表理解应该更快.
>>> [element for list_ in s for element in list_]
['a', 'b', 'c', 'd', 'f', 'g']
>>> %timeit -n 100000 [element for list_ in s for element in list_]
100000 loops, best of 3: 5.2 µs per loop
>>> %timeit -n 100000 s.sum()
100000 loops, best of 3: 50.7 µs per loop
直接访问列表的值甚至更快.
>>> %timeit -n 100000 [element for list_ in s.values for element in list_]
100000 loops, best of 3: 2.77 µs per loop