distinct column values pandas code example

Example 1: how to get distinct value in a column dataframe in python

df.column.unique()

Example 2: pandas count distinct values in a column

Series.value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True)

Example 3: if df[col].unique()==2

df_dict = dict(zip([i for i in df.columns] , [pd.DataFrame(df[i].unique(), columns=[i]) for i in df.columns]))

Example 4: unique entries in column pandas

# Import modules
import pandas as pd

# Set ipython's max row display
pd.set_option('display.max_row', 1000)

# Set iPython's max column width to 50
pd.set_option('display.max_columns', 50)
# Create an example dataframe
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'year': [2012, 2012, 2013, 2014, 2014], 
        'reports': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df

#List unique values in the df['name'] column
df.name.unique()