Let’s see how can we retrieve the unique values from pandas dataframe.
Let’s create a dataframe from CSV file. We are using the past data of GDP from different countries. You can get the dataset from here.
# import pandas as pd import pandas as pd # load the data with pd.read_csv record = pd.read_csv(gapminder_csv_url) record.head() |

Method #1: Select the continent column from the record and apply the unique function to get the values as we want.
# import pandas as pd import pandas as pd # load the data with pd.read_csv record = pd.read_csv(gapminder_csv_url) print(record['continent'].unique()) |
Output:
['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']
Method #2: Select unique values from the countrycolumn.
# import pandas as pd import pandas as pd # load the data with pd.read_csv record = pd.read_csv(gapminder_csv_url) print(record.country.unique()) |
Output:
['Afghanistan' 'Albania' 'Algeria' 'Angola' 'Argentina' 'Australia' 'Austria' 'Bahrain' 'Bangladesh' 'Belgium' 'Benin' 'Bolivia' 'Bosnia and Herzegovina' 'Botswana' 'Brazil' 'Bulgaria' 'Burkina Faso' 'Burundi' 'Cambodia' 'Cameroon' 'Canada' 'Central African Republic' 'Chad' 'Chile' 'China' 'Colombia' 'Comoros' 'Congo Dem. Rep.' 'Congo Rep.' 'Costa Rica' "Cote d'Ivoire" 'Croatia' 'Cuba' 'Czech Republic' 'Denmark' 'Djibouti' 'Dominican Republic' 'Ecuador' 'Egypt' 'El Salvador' 'Equatorial Guinea' 'Eritrea' 'Ethiopia' 'Finland' 'France' 'Gabon' 'Gambia' 'Germany' 'Ghana' 'Greece' 'Guatemala' 'Guinea' 'Guinea-Bissau' 'Haiti' 'Honduras' 'Hong Kong China' 'Hungary' 'Iceland' 'India' 'Indonesia' 'Iran' 'Iraq' 'Ireland' 'Israel' 'Italy' 'Jamaica' 'Japan' 'Jordan' 'Kenya' 'Korea Dem. Rep.' 'Korea Rep.' 'Kuwait' 'Lebanon' 'Lesotho' 'Liberia' 'Libya' 'Madagascar' 'Malawi' 'Malaysia' 'Mali' 'Mauritania' 'Mauritius' 'Mexico' 'Mongolia' 'Montenegro' 'Morocco' 'Mozambique' 'Myanmar' 'Namibia' 'Nepal' 'Netherlands' 'New Zealand' 'Nicaragua' 'Niger' 'Nigeria' 'Norway' 'Oman' 'Pakistan' 'Panama' 'Paraguay' 'Peru' 'Philippines' 'Poland' 'Portugal' 'Puerto Rico' 'Reunion' 'Romania' 'Rwanda' 'Sao Tome and Principe' 'Saudi Arabia' 'Senegal' 'Serbia' 'Sierra Leone' 'Singapore' 'Slovak Republic' 'Slovenia' 'Somalia' 'South Africa' 'Spain' 'Sri Lanka' 'Sudan' 'Swaziland' 'Sweden' 'Switzerland' 'Syria' 'Taiwan' 'Tanzania' 'Thailand' 'Togo' 'Trinidad and Tobago' 'Tunisia' 'Turkey' 'Uganda' 'United Kingdom' 'United States' 'Uruguay' 'Venezuela' 'Vietnam' 'West Bank and Gaza' 'Yemen Rep.' 'Zambia' 'Zimbabwe']
Method #3:
In this method you can see that we use the dataframe inside the unique function as parameter although we select the same column as above so we get the same output.
# Write Python3 code here # import pandas as pd import pandas as pd # load the data with pd.read_csv record = pd.read_csv(gapminder_csv_url) print(pd.unique(record['continent'])) |
Output:
['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']
Recommended Posts:
- Get unique values from a column in Pandas DataFrame
- Get column index from column name of a given Pandas DataFrame
- Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Get n-smallest values from a particular column in Pandas DataFrame
- Get n-largest values from a particular column in Pandas DataFrame
- How to Concatenate Column Values in Pandas DataFrame?
- Get a list of a particular column values of a Pandas DataFrame
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- How to Count Distinct Values of a Pandas Dataframe Column?
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Mapping external values to dataframe values in Pandas
- Highlight the negative values red and positive values black in Pandas Dataframe
- Create a DataFrame from a Numpy array and specify the index column and column headers
- How to Drop rows in DataFrame by conditions on column values?
- How to get column names in Pandas dataframe
- Capitalize first letter of a column in Pandas dataframe
- Python | Change column names and row indexes in Pandas DataFrame
- Convert the column type from string to datetime format in Pandas dataframe
- Apply uppercase to a column in Pandas dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

