![]() |
VOOZH | about |
When working with data in Pandas, it’s often useful to know what unique values exist in a column. For example, you might want to know all the continents or countries present in a dataset. Pandas provides simple methods to retrieve these unique values quickly.
We will use a GDP dataset from different countries to demonstrate.
To download the dataset used in this article, click here
Output
You can select a column and call unique() to get all unique values in that column.
['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']You can also use the same method to get all unique countries:
This will return all countries in the dataset as a NumPy array.
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']
Pandas also provides a standalone pd.unique() function that takes a column (or Series) as input:
Output
['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']