Recipe: How to list unique values in a Pandas DataFrame?
DATA MUNGING PYTHON PANDAS DATAFRAME PANDAS CHEATSHEET PANDAS DATAFRAME TUTORIAL

How to list unique values in a Pandas DataFrame?

This recipe helps you list unique values in a Pandas DataFrame
In [1]:
## How to list unique values in a Pandas DataFrame
def Kickstarter_Example_92():
    print()
    print(format('How to list unique values in a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    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', 'Tina', 'Amy'],
            'year': [2012, 2012, 2013, 2014, 2014],
            'reports': [4, 24, 31, 2, 3]}
    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz',
                                     'Maricopa', 'Yuma'])
    print(); print(df)

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

Kickstarter_Example_92()
*****************How to list unique values in a Pandas DataFrame******************

             name  year  reports
Cochice     Jason  2012        4
Pima        Molly  2012       24
Santa Cruz   Tina  2013       31
Maricopa     Tina  2014        2
Yuma          Amy  2014        3

['Jason' 'Molly' 'Tina' 'Amy']


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from C-DAC
1 developer from HvH
1 developer from Kenya Commercial Bank
1 developer from Nokia
1 developer from Tata Consultancy Services
1 developer from Accenture
1 developer from Candela Labs
1 developer from ICFAI
1 developer from KPMG
1 developer from Renovite Technologies