如何从由术语列表形成的熊猫系列中获取一组

Fer*_*ino 3 python set pandas

我有一个由术语列表组成的熊猫系列:

my_series = pd.Series([['a','b','c'], ['a','d'], [], ['e']])
Run Code Online (Sandbox Code Playgroud)

是否有更好/更优雅/更快的方式来获得一组独特的术语而不是这样做?:

lt = set()
for l in my_series.tolist():
    lt = lt.union(l)
Run Code Online (Sandbox Code Playgroud)

cs9*_*s95 5

O(n)扩展可迭代解包set.union.

>>> set().union(*my_series)
{'a', 'b', 'c', 'd', 'e'}
Run Code Online (Sandbox Code Playgroud)

如果你更喜欢老式的,那么集合理解就相当了 -

>>> {y for x in my_series for y in x}
{'a', 'b', 'c', 'd', 'e'}
Run Code Online (Sandbox Code Playgroud)