You can use the function unique() For the Python Pandas data to determine the unique values in a column. This effectively gets an overview of the different values within a set of data.
Web accommodation
Flexible, efficient and safe web accommodation
- SSL certificate and DDOS protection
- Data backup and restoration
- Assistance 24/7 and personal advisor
The syntax of the function DataFrame[].unique() pandas
Basic syntax for the use of pandas unique() is simple, because the function takes no parameter:
DataFrame['nom_colonne'].unique()
python
Note that for the dataframes of pandas, unique() can only apply to A specific column. You must therefore specify it beforehand. The function unique() send you a numpy table with unique values in the order of appearance; There is no sorting of values.
Note
If you have long been interested in Python programming, you certainly also know the equivalent NUMPY OF THE FUNCTION unique() pandas. The variant of pandas is however preferable for reasons of efficiency.
Function application unique() pandas
On pandas data, you can use unique() By first specifying the column in which you want to search for unique values.
In the following example, we consider a dataframe containing information on different people.
import pandas as pd
# Créer un exemple de DataFrame
data = {
'Nom' : ['Alice', 'Bob', 'Charlie', 'David', 'Edward'],
'Âge' : [24, 27, 22, 32, 29],
'Ville' : ['New York', 'Los Angeles', 'New York', 'Chicago', 'Los Angeles']
}
df = pd.DataFrame(data)
print(df)
python
Dataframa then presents itself as follows:
Nom Âge Ville
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 New York
3 David 32 Chicago
4 Edward 29 Los Angeles
It is now a question of determining the cities in which these people live. For that, Each city should only be listed once. The function unique() de Pandas is applied to the DataFrame column which contains the cities:
# Trouver les villes uniques dans la colonne « Ville »
unique_cities = df['Ville'].unique()
print(unique_cities)
python
The result is a NUMPY table which contains each city once. This shows that people come from three different cities: New York, Los Angeles and Chicago.
['New York' 'Los Angeles' 'Chicago']

