PYTHON
get list of unique values in pandas column
a = df['column name'].unique() #returns a list of unique values
How to Find Unique Values in a Column in Pandas
# 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())
how to get unique value of all columns in pandas
print(df.apply(lambda col: col.unique()))
pandas unique values to list
df.groupby('param')['column'].nunique().sort_values(ascending=False).unique().tolist()
dataframe python unique values rows
# get the unique values (rows)
df.drop_duplicates()
Find unique values in all columns in Pandas DataFrame
# 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())
get unique values from a list
mylist = ['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
myset = set(mylist)
print(myset)
mynewlist = list(myset)
#['nowplaying', 'PBS', 'job', 'debate', 'thenandnow']