Saturday, January 15, 2022

ItsMyCode: Pandas : How to Find Unique Values in a Column

ItsMyCode |

We can find unique values of a column in Pandas DataFrame using the unique() function.

The unique() method filters out only unique values from a column. In this tutorial, we will learn how to use the unique() method to find unique values in Pandas DataFrame with examples.

We have a simple DataFrame with the dictionary of lists, indicates the fruits, price and quantity as the column names.

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

print(df)

Output

   fruits price quantity
0  orange    40      200
1   mango    80      300
2   apple    30      300
3  grapes    40      400
4  orange    30      200
5   mango    80      800

Find unique values of a single column in Pandas DataFrame

Let’s say if we need to find the unique values of a fruits column, we can use the unique() method as shown in the below code.

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the unique value of column fruits
print(df.fruits.unique())

Output

['orange' 'mango' 'apple' 'grapes']

Find unique values in all columns in Pandas DataFrame

If we need to find the unique values of all the columns in Pandas DataFrame, we need to iterate the columns using the for loop and then use the unique() method on each column name.

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the unique value of all columns
for col in df:
  print(df                      
                .unique())

Output

['orange' 'mango' 'apple' 'grapes']
['40' '80' '30']
['200' '300' '400' '800']

Find and count unique values of a single column in Pandas DataFrame

We can even count the occurrence of unique values in a single column using the method value_counts() method.

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'fruits': ['orange', 'mango', 'apple', 'grapes', 'orange', 'mango'],
                   'price': ['40', '80', '30', '40', '30', '80'],
                   'quantity': ['200', '300', '300', '400', '200', '800']
                   })

# get the count unique values of column fruits
print(df.fruits.value_counts())

Output

orange    2
mango     2
apple     1
grapes    1
Name: fruits, dtype: int64

The post Pandas : How to Find Unique Values in a Column appeared first on ItsMyCode.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...