How to get a set from a Pandas series formed by lists of terms

  • A+
Category:Languages

I have a Pandas Series formed by list of terms:

my_series = pd.Series([['a','b','c'], ['a','d'], [], ['e']]) 

Is there a better/more elegant/faster way of get a set of unique terms than doing like this?:

lt = set() for l in my_series.tolist():     lt = lt.union(l) 


O(n) extended iterable unpacking with set.union.

>>> set().union(*my_series) {'a', 'b', 'c', 'd', 'e'} 

If you prefer old-fashioned, there's the set-comprehension equivalent -

>>> {y for x in my_series for y in x} {'a', 'b', 'c', 'd', 'e'} 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: