PYTHON
dataframe unique values in each column
for col in df:
print(df[col].unique())
count unique values in pandas column
df['column_name'].value_counts()
how to count unique values in a column dataframe in python
dataframe.column.nunique()
how to count unique values in dataframe df python
#count unique values in each column
df.nunique()
#count unique values in each row
df.nunique(axis=1)
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()))
Getting unique values in a column
# 10. Getting unique names of values in a column
df['Airline'].unique()
python - subset dataframe based on unique value of a clumn
# Keep first duplicate value
my_df = my_df.drop_duplicates(subset=['my_var'])
# Keep last duplicate value
my_df = my_df.drop_duplicates(subset=['my_var'], keep='last')
# Remove all duplicate values
my_df = my_df.drop_duplicates(subset=['my_var'], keep=False)
unique values in dataframe column count
df.groupby('mID').agg(['count', 'size', 'nunique']).stack()
dID hID uID
mID
A count 5 5 5
size 5 5 5
nunique 3 5 5
B count 2 2 2
size 2 2 2
nunique 2 2 2
C count 1 1 1
size 1 1 1
nunique 1 1 1
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())
unique rows in dataframe
In [33]: df[df.columns[df.apply(lambda s: len(s.unique()) > 1)]]
Out[33]:
A B
0 0 a
1 1 b
2 2 c
3 3 d
4 4 e
dataframe number of unique rows
1
# get the unique values (rows) by retaining last row
2
df.drop_duplicates(keep='last')
pandas get number unique values in column
df["Your_Column"].nunique()