• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / How to Get Unique Values from a Column in Pandas Data Frame?

How to Get Unique Values from a Column in Pandas Data Frame?

January 31, 2018 by cmdlinetips

In this tutorial, we will learn how to get unique values of a column in a Pandas dataframe using two approaches. We will first use Pandas unique() function to get unique values of a column and then use Pandas drop_duplicates() function to get unique values of a column.

Pandas unique() function To Get Unique values of a Column in Pandas?

Pandas unique() function can work with dataframe and Series object and give unique values of a variable. In this example we will learn how to use Pandas unique() function on a column of a dataframe.

For example, let us say we want to find the unique values of column ‘continent’ in the data frame.This would result in all continents in the dataframe. We can use pandas’ function unique() on the column of interest. And it will return NumPy array with unique values of the column.

Pandas unique values examples using gapminder data set

Let us get started with some examples from a real world data set. We will use gapminder dataset to get unique values of character/categorical variable.

# import pandas as pd
import pandas as pd
# software carpentry url for gapminder data
gapminder_csv_url ='http://bit.ly/2cLzoxH'
# load the data with pd.read_csv
gapminder = pd.read_csv(gapminder_csv_url)

Let us check the basic information of the data frame. We can see that the variables ‘continent’ and ‘country’ are objects/strings and we can find the number of unique values for them.

# check the data frame info
print(gapminder.info())

class 'pandas.core.frame.DataFrame'>
RangeIndex: 1704 entries, 0 to 1703
Data columns (total 6 columns):
country      1704 non-null object
year         1704 non-null int64
pop          1704 non-null float64
continent    1704 non-null object
lifeExp      1704 non-null float64
gdpPercap    1704 non-null float64
dtypes: float64(3), int64(1), object(2)
memory usage: 79.9+ KB

Applying Pandas unique() to a column of a dataframe

>gapminder['continent'].unique()
array(['Asia', 'Europe', 'Africa', 'Americas', 'Oceania'], dtype=object)

Note that the unique values of a column from Pandas unique() function is not sorted and it will be returned in order of appearance in the dataframe.

We can also use Pandas chaining method and use it on the Pandas Series corresponding to the column and get unique values.

>gapminder.continent.unique()
array(['Asia', 'Europe', 'Africa', 'Americas', 'Oceania'], dtype=object)

Unique values of a columns as a list

If we want the the unique values of the column in pandas data frame as a list, we can easily apply the function tolist() by chaining it to the previous command.

>gapminder['continent'].unique().tolist()
['Asia', 'Europe', 'Africa', 'Americas', 'Oceania']

If we try the unique function on the ‘country’ column from the dataframe, the result will be a big numpy array.

>gapminder['country'].unique()

Instead, we can simply count the number of unique values in the country column and find that there are 142 countries in the data set.

>len(gapminder['country'].unique().tolist())
142

How To Get Unique Values of a Column with drop_duplicates()

Another way, that is a bit unintuitive , to get unique values of column is to use Pandas drop_duplicates() function in Pandas. Pandas’ drop_duplicates() function on a variable/column removes all duplicated values and returns a Pandas series.

For example, to get unique values of continent variable, we will Pandas’ drop_duplicates() function as follows.

# unique values with drop_duplicates
gapminder.continent.drop_duplicates()

0         Asia
12      Europe
24      Africa
48    Americas
60     Oceania
Name: continent, dtype: object

Want to learn more Pandas tips? Check out our new Byte Sized Pandas 101 tutorials.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Add a New Column Using a Dictionary in Pandas Data Frame ? Default ThumbnailHow to Get Frequency Counts of a Column in Pandas Dataframe: Pandas Tutorial Pandas Filter/Select Rows Based on Column ValuesHow To Filter Pandas Dataframe By Values of Column? Default ThumbnailHow To Read a CSV File as Pandas Data Frame?

Filed Under: Pandas DataFrame, Python, Python Tips Tagged With: Pandas Data Frame, Pandas drop_duplicates(), Pandas unique(), Python Tips, Unique Values Pandas, uniques values in a column pandas

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version