How to Get Unique Values in Column of Pandas DataFrame

In this post, we will see how to get Unique Values from a Column in Pandas DataFrame.

Sometimes, You might want to get unique Values from a Column in large Pandas DataFrame.

Here is a sample Employee data which we will use.

NameAgeDepartment
Rohit26Sales
John21HR
Appy34Sales
Smita47Tech
Neha22HR

Using unique() method

You can use Pandas unique() method to get unique Values from a Column in Pandas DataFrame.
Here is an example. We will use unique() method to get unique value from Department column.

Output:

As you can see, we got unique values for Department column.
If you want the result as list, you can use toList() function to convert numpy.ndarray to list.
Change highlight line with below line:

Output:

Using drop_duplicates() method

You can also use drop_duplicates() to get unique values from a column in Pandas DataFrame. Although this method does not obvious as compared to unique one.

Here is an example. We will use drop_duplicates() method to get unique value from Department column.

Output:

As you can see, we got Series as output by using drop_duplicates() method.

Related Posts

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in Pandas A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from external files and […]

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.