How to list unique values in a Pandas DataFrame?
0

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']