. Advertisement .
..3..
. Advertisement .
..4..
Pandas get unique values in column is an important task in Pandas programs, though not many users know tips to do so efficiently. Our guidelines will shed some light on this dilemma. Let’s get started!
Tips on Pandas Get Unique Values in Column
Before we start, let’s create an illustrative DataFrame, which you can refer to throughout this guideline:
import pandas as pd
# dataframe of height and weight football players
df = pd.DataFrame({
'Height': [166, 174, 169, 185, 189, 187, 157, 168, 182, 179],
'Weight': [64, 69, 71, 79, 85, 93, 49, 57, 77, 84],
'Team': ['B', 'B', 'C', 'C', 'C', 'A', 'B', 'A', 'C', 'A']
})
# display the dataframe
print(df)
Here is the output:
Height Weight Team
0 166 64 B
1 174 69 B
2 169 71 C
3 185 79 C
4 189 85 C
5 187 93 A
6 157 49 B
7 168 57 A
8 182 77 C
9 179 84 A
You can see that it is a complete DataFrame with three columns and ten rows, storing important information on weight, height, and all the top scorers of a huge football competition. Let’s get on with the methods we are going to introduce in the next section.
Method 1. List All Unique Values in One DataFrame Column
It is possible to count on the unique()
functions to receive unique values available in one column. It will send you NumPy arrays of unique column values. Let’s say you want to find all unique values from the “Team column” of our established DataFrame above.
# unique values in column "Team"
print(df['Team'].unique())
# check the return type
print(type(df['Team'].unique()))
The output will be:
['B' 'C' 'A']
<class 'numpy.ndarray'>
As you see, we have an array containing all unique values available from the “Team” column – “B”, “C”, and “A”.
Method 2. Count The Columns Unique Values
Let’s say you don’t want a list of unique values like in method 1; you only want the count number. In that case, we suggest you use nunique()
functions, which send you the unique value count in your DataFrame as one integer.
Suppose we want to count all unique values from “Height” on the above DataFrame. Apply this code:
# count of unique values
print(df['Height'].nunique())
Output:
10
Method 3. Count The Occurrences of Every Unique Value from One Column
In this method, we will use the value_counts()
functions to know how many times each value occurs. For instance, let’s say you want to know the counts of every unique value in “Height”.
# value counts of each unique value
print(df['Height'].value_counts())
Output:
166 1
174 1
169 1
185 1
189 1
187 1
157 1
168 1
182 1
179 1
Name: Height, dtype: int64
You may see that every value in this column occurs one time. Overall, value_counts()
is quite helpful in checking data point distribution across one categorical field.
Conclusion
Our article has introduced three methods for Pandas get unique values in Column, each with a detailed example. ITtutoria hopes you can find some great tips from this tutorial. For similar issues related to Pandas columns (such as slicing columns), you can browse the website for more guidance.
Leave a comment