# Create a Set From a Series in Pandas

• Post author:
• Post category:Pandas / Python

We can create a set from a series of pandas by using `set()`, `Series.unique()` function. The set object is used to store multiple items which are heterogeneous. Just like a list, tuple, and dictionary, the set is another built-in data type in python which is used to store elements without duplicates.

In this article, I will explain how to create a set from a series of pandas by using the set(), Series.unique() function.

## 1. Quick Examples of Creating a Set From a Series

If you are in a hurry, below are some quick examples of how to create a set object from a series in pandas.

``````
# Below are the quick examples.

# Example 1: using series.unique() & set() function
setObj = ser.unique()
ser2 = set(setObj)
print(ser2)

# Example 2: using set() function to create Set
ser2 = set(ser)
print(ser2)
``````

## 2. Create Pandas Series

Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the data types such as strings, integers, float, and other python objects. We can access each element in the Series with the help of corresponding default indices.

Now, let’s create pandas series using a list of values.

``````
import pandas as pd
# Create the Series
ser = pd.Series([20,25,15,10,5,20,30])
print(ser)
``````

Yields below output.

``````
# Output:
0    20
1    25
2    15
3    10
4     5
5    20
6    30
dtype: int64
``````

## 3. Create a Set from Pandas Series

Set is used to store unique values hence it is best practice to remove duplicates from the pandas Servies using Series.unique() function and then create a Set object. Series.unique() function returns the NumPy Array object.

``````
# use series.unique() function
ser2 = ser.unique()
print(ser2)

# Output
# [20 25 15 10  5 30]

# using series.unique() & set() function
ser.unique()
ser2 = set(ser)
print(ser2)

# Output
# {5, 10, 15, 20, 25, 30}
``````

The `set()` function also removes all duplicate values and gets only unique values from Series. We can use this `set()` function to get unique values from Series. For examples.

``````
Using set() function to create Set
ser2 = set(ser)
print(ser2)
``````

Yields below output.

``````
# Output
{5, 10, 15, 20, 25, 30}
``````

## 5. Complete Example For Creating a Set from a Series

``````
import pandas as pd

# Create the Series
ser = pd.Series([20,25,15,10,5,20,30])
print(ser)

# use series.unique() function
ser2 = ser.unique()
print(ser2)

# using series.unique() & set() function
ser.unique()
ser2 = set(ser)
print(ser2)

# Using set() function to create Set
ser2 = set(ser.unique())
print(ser2)
``````

## 6. Conclusion

In this article, I have explained how to create a set from a Pandas Series using` set()`, `Series.unique()` functions with examples.

Happy Learning !!

## References
{"ft_lang_label":"__label__en","ft_lang_prob":0.615413,"math_prob":0.72316045,"size":2952,"snap":"2023-40-2023-50","text_gpt3_token_len":783,"char_repetition_ratio":0.18724559,"word_repetition_ratio":0.16129032,"special_character_ratio":0.28184283,"punctuation_ratio":0.13926175,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98562425,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-23T01:46:58Z\",\"WARC-Record-ID\":\"<urn:uuid:3edba008-af45-4a8c-942a-fed3c969682f>\",\"Content-Length\":\"228485\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5dad5d2-f9e2-4dac-adf7-4dd1301c822b>\",\"WARC-Concurrent-To\":\"<urn:uuid:7ba006f2-ecc5-43f8-977b-2ac38b8bcb38>\",\"WARC-IP-Address\":\"104.21.17.158\",\"WARC-Target-URI\":\"https://sparkbyexamples.com/pandas/create-set-from-series-in-pandas/\",\"WARC-Payload-Digest\":\"sha1:ZVXH24CJZJNTQPTHH3UU5LHZOTN6MDEX\",\"WARC-Block-Digest\":\"sha1:LCA4BRCHTEUL7EG6D7PKAYRZHF23TTYH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506429.78_warc_CC-MAIN-20230922234442-20230923024442-00510.warc.gz\"}"}