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

Kurssisisältö

Pandas First Steps

Pandas First Steps

1. The Very First Steps
2. Reading Files in Pandas
3. Analyzing the Data

book
Unique 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

Now, we'll 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

To count the number of distinct values in a specific column, you can use the nunique() method:

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
Tehtävä

Swipe to start coding

You are given a DataFrame named audi_cars.

  • Identify all distinct values in the 'year' column and store the result in the unique_years column.
  • Identify all distinct values in the 'fueltype' column and store the result in the unique_fueltype variable.
  • Determine the number of unique fuel types and store the result in the count_unique_fueltypes variable.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 15
toggle bottom row

book
Unique 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

Now, we'll 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

To count the number of distinct values in a specific column, you can use the nunique() method:

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
Tehtävä

Swipe to start coding

You are given a DataFrame named audi_cars.

  • Identify all distinct values in the 'year' column and store the result in the unique_years column.
  • Identify all distinct values in the 'fueltype' column and store the result in the unique_fueltype variable.
  • Determine the number of unique fuel types and store the result in the count_unique_fueltypes variable.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 15
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt