Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Unique Values | Analyzing the Data
Pandas First Steps

bookUnique Values

Data often gets duplicated in DataFrames. For instance, in the countries DataFrame, the 'continent' column has repeated entries. There's a method that retrieves an array of distinct values from a specific DataFrame column.

1234567
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) print(countries)
copy

Next, apply the unique() method to the 'continent' and 'country' columns:

12345678910
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) unique_countries = countries['country'].unique() unique_continents = countries['continent'].unique() print(unique_countries) print(unique_continents)
copy

Sometimes, instead of retrieving all unique values, you might just want to know how many distinct values exist in a column. In such cases, you can use the nunique() method. It returns the count of unique entries in a column β€” not the values themselves.

1234567
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) print(countries['continent'].nunique())
copy
Task

Swipe to start coding

You are given a DataFrame named audi_cars. Your goal is to explore its data and identify unique values using Pandas methods.

  1. Retrieve all distinct values from the 'year' column and store them in the variable unique_years.
  2. Retrieve all distinct values from the 'fueltype' column and store them in the variable unique_fueltype.
  3. Determine the number of unique fuel types in the 'fueltype' column using the .nunique() method and store the result in the variable count_unique_fueltypes.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 15
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 3.03

bookUnique Values

Swipe to show menu

Data often gets duplicated in DataFrames. For instance, in the countries DataFrame, the 'continent' column has repeated entries. There's a method that retrieves an array of distinct values from a specific DataFrame column.

1234567
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) print(countries)
copy

Next, apply the unique() method to the 'continent' and 'country' columns:

12345678910
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) unique_countries = countries['country'].unique() unique_continents = countries['continent'].unique() print(unique_countries) print(unique_continents)
copy

Sometimes, instead of retrieving all unique values, you might just want to know how many distinct values exist in a column. In such cases, you can use the nunique() method. It returns the count of unique entries in a column β€” not the values themselves.

1234567
import pandas as pd country_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(country_data) print(countries['continent'].nunique())
copy
Task

Swipe to start coding

You are given a DataFrame named audi_cars. Your goal is to explore its data and identify unique values using Pandas methods.

  1. Retrieve all distinct values from the 'year' column and store them in the variable unique_years.
  2. Retrieve all distinct values from the 'fueltype' column and store them in the variable unique_fueltype.
  3. Determine the number of unique fuel types in the 'fueltype' column using the .nunique() method and store the result in the variable count_unique_fueltypes.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 15
single

single

some-alt