Create a set from a series in pandas

PythonPandasDataframeSeriesKaggle

Python Problem Overview


I have a dataframe extracted from Kaggle's San Fransico Salaries: https://www.kaggle.com/kaggle/sf-salaries and I wish to create a set of the values of a column, for instance 'Status'.

This is what I have tried but it brings a list of all the records instead of the set (sf is how I name the data frame).

a=set(sf['Status'])
print a

According to this webpage, this should work. https://stackoverflow.com/questions/15768757/how-to-construct-a-set-out-of-list-items

Python Solutions


Solution 1 - Python

If you only need to get list of unique values, you can just use unique method. If you want to have Python's set, then do set(some_series)

In [1]: s = pd.Series([1, 2, 3, 1, 1, 4])

In [2]: s.unique()
Out[2]: array([1, 2, 3, 4])

In [3]: set(s)
Out[3]: {1, 2, 3, 4}

However, if you have DataFrame, just select series out of it ( some_data_frame['<col_name>'] ).

Solution 2 - Python

With large size series with duplicates the set(some_series) execution-time will evolve exponentially with series size.

Better practice would be to set(some_series.unique()).

A simple exemple showing x16 execution time.enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulio ArriagaView Question on Stackoverflow
Solution 1 - PythongrechutView Answer on Stackoverflow
Solution 2 - PythonAdrien PacificoView Answer on Stackoverflow