Pandas: Unique Function

Here we want to pull unique values from the Years column in our table. When we say unique we want to get the individual values of Years in the column. Sometimes you will have data comeback and there are duplicate names, numbers or any type of object. Using the “unique” function in the pandas library we can consolidate the duplicated data to get a single instance of it.

Example Of Use:

An example would be a data set with a list of names in it. Our client wants us to send them a list of all the people that used a credit card at a private event and we will make the assumption that no guest had the same name. We pull the data in and we get this list of names:

Name = (“Sherry”,”Paul”,”Jared”,”Peter”,”Paul”,”Justin”)< 5 names

Next we want to take the list of names and do 2 things. We want to find out how many people used their credit card at the event and also send a Thank You notice to each of them. Using the “unique” function we can prevent any duplicates so the same person will:

  1. Not recieve multiple Thank You Cards.
  2. We can get an accurate count of how many people used their card.

Using the “unique” function we will get a returned list:

Name = (“Sherry”,”Paul”,”Jared”,”Peter”,”Justin”)

Python Code:

1
2
3
import pandas
variablename_df = pandas.read_csv('yourspreadsheet.csv')
print (variablename_df.Name.unique())

Source: pandas.unique – Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *