+3 votes
in Programming Languages by (9.6k points)
edited by
I want to know the unique elements in each column of a Pandas DataFrame. How can I find them?

1 Answer

+3 votes
by (42.1k points)
edited by

I am not sure if there is any function for it. You can select one column of a DataFrame and then apply the unique() function on the column to find the unique values in it.

Here is a Python code that prints unique elements in each column:

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3, 4, 1, 2, 2, 1], 'b': [8, 8, 4, 5, 3, 2, 3, 4]})
for col in df.columns:
    print("Unique values in column {0}: {1}".format(col, df[col].unique()))

Like the python dictionary, you can use items() on the DataFrame to iterate over (column name, Series) pairs. You can apply the unique() function on the Series to find the unique values in columns.

Here is an example to show how to use items():

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3, 4, 1, 2, 2, 1], 'b': [8, 8, 4, 5, 3, 2, 3, 4]})

for col, vals in df.items():
    print("Unique values in column {0}: {1}".format(col, vals.unique()))
 

The above codes will print the following output:

Unique values in column a: [1 2 3 4]
Unique values in column b: [8 4 5 3 2]

...