Create a Set from a Pandas Series


This entry is part 4 of 4 in the series pandas Series

A pandas DataFrame can be thought of as a group of Series. Each column is a Series. What is a set? A set is an unordered collection of unique elements. You can use sets in your exploratory data analysis (EDA). How?

Suppose you have a column in a DataFrame that you want to investigate. You want to look at some of the lowest values in the column. Suppose the minimum value is zero. You are a bit surprised because there shouldn’t be any zeros in the data. Is it possible that some of those values are not actually zero, but very small numbers that were rounded down to zero. You want to take a look at some of those small values near zero.

What would the syntax look like? Suppose your DataFrame is called df and a column in it is called my_column, and suppose you wanted the first ten items.

sorted(set(df['my_column']))[:10]
Series Navigation<< Pandas Series from Dictionary

Leave a Reply