Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a set from a series in pandas

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. How to construct a set out of list items in python?

like image 425
Julio Arriaga Avatar asked Sep 17 '16 20:09

Julio Arriaga


People also ask

Can we create DataFrame from series?

You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame.

How do you convert a series to a DataFrame in python?

to_frame() function is used to convert the given series object to a dataframe. Parameter : name : The passed name should substitute for the series name (if it has one). Example #1: Use Series.

How do I convert a Pandas series to a list?

How to use the tolist() method to convert pandas series to list. To convert a pandas Series to a list, simply call the tolist() method on the series which you wish to convert.

How do you create a series on Pandas?

You can create a series by calling pandas. Series() . An list, numpy array, dict can be turned into a pandas series. You should use the simplest data structure that meets your needs.


1 Answers

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>'] ).

like image 159
grechut Avatar answered Sep 18 '22 14:09

grechut