9

Pandas Series.nunique() – thisPointer.com

 3 years ago
source link: https://thispointer.com/pandas-series-nunique-python-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This article explains the usage details of Pandas.Series.nunique() in Python with a few examples.

In Pandas, the Series class provides a member function nunique(), which returns a count of unique elements.

pandas.Series.nunique()

Series.nunique(dropna=True)
Series.nunique(dropna=True)
  • Returns:
    • The number of unique values in the Series.

By default, it excludes the NaN while counting unique values. If you want to include NaN, then pass the dropna argument with value False.

Examples of Series.nunique() function

First, we will create a Series object from a list,

import pandas as pd
import numpy as np
# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])
print(seres_obj)
import pandas as pd
import numpy as np

# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])

print(seres_obj)

Output:

0 11.0
1 23.0
3 56.0
5 34.0
6 55.0
7 11.0
9 56.0
10 34.0
dtype: float64
0     11.0
1     23.0
2      4.0
3     56.0
4      NaN
5     34.0
6     55.0
7     11.0
8      4.0
9     56.0
10    34.0
dtype: float64

Our Series object contains many duplicate elements. Now let’s call the nunique() function on this Series object,

# Get Count of Unique elements in Series
count = seres_obj.nunique()
print('Count of Unique values: ', count)
# Get Count of Unique elements in Series
count = seres_obj.nunique() 

print('Count of Unique values: ', count)

Output:

Count of Unique values: 6
Count of Unique values:  6

It returned a count of the unique values from the Series object. By default, it excluded the NaN from the calculation. Let’s see another example where we will include NaN values too.

Examples of Series.nunique() with dropna

import pandas as pd
import numpy as np
# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])
print(seres_obj)
# Get Count of Unique elements in Series including NaN
count = seres_obj.nunique(dropna=False)
print('Count of Unique values: ', count)
import pandas as pd
import numpy as np

# Create Series object from List
seres_obj = pd.Series([11, 23, 4, 56, np.NaN, 34, 55, 11, 4, 56, 34])

print(seres_obj)

# Get Count of Unique elements in Series including NaN
count = seres_obj.nunique(dropna=False) 

print('Count of Unique values: ', count)

Output:

0 11.0
1 23.0
3 56.0
5 34.0
6 55.0
7 11.0
9 56.0
10 34.0
dtype: float64
Count of Unique values: 7
0     11.0
1     23.0
2      4.0
3     56.0
4      NaN
5     34.0
6     55.0
7     11.0
8      4.0
9     56.0
10    34.0
dtype: float64

Count of Unique values:  7

As we passed the dropna argument with value False to the nunique() function. Therefore, it returned the count of unique values in Series, including NaN.

Another example of Pandas.Series.nunique()

Let’s see another example, where we will create a Pandas Series of strings and then fetch the count of unique elements from Series using nunique() function. For example,

import pandas as pd
# Create Series object from List
names = pd.Series([ 'Ritika',
'John',
'Ritika',
'Shaun',
'John',
'Ritika',
'Mark',
'Shaun',
print(names)
# Get Count of Unique elements in Series
count = names.nunique()
print('Count of Unique Names: ', count)
import pandas as pd

# Create Series object from List
names = pd.Series([ 'Ritika',
                    'John',
                    'Ritika',
                    'Shaun',
                    'John',
                    'Ritika',
                    'Mark',
                    'Shaun',
                    ])

print(names)

# Get Count of Unique elements in Series
count = names.nunique() 

print('Count of Unique Names: ', count)

Output:

0 Ritika
1 John
2 Ritika
3 Shaun
4 John
5 Ritika
6 Mark
7 Shaun
dtype: object
Count of Unique Names: 4
0    Ritika
1      John
2    Ritika
3     Shaun
4      John
5    Ritika
6      Mark
7     Shaun
dtype: object

Count of Unique Names:  4

Summary:

Today we learned how to use the nunique() function of the Pandas series.


Recommend

  • 8

    This article explains the usage details of Pandas.Series.is_unique in Python with few examples. In Pandas, the Series class provides a member variable is_unique, whose value will return True if all Series elements are unique. p...

  • 5

    This article explains the usage details of Pandas.Series.unique() in Python with few examples. In Pandas, the Series class provides a member function unique(), which returns a numpy array of unique elements in the Series.

  • 3
    • thispointer.com 3 years ago
    • Cache

    Pandas Dataframe.loc[] – thisPointer

    Pandas Dataframe.loc[] – thisPointer Skip to content In this article, we will discuss how to use the loc property of the Dataframe with examples. In Pandas, t...

  • 4
    • thispointer.com 3 years ago
    • Cache

    Pandas Dataframe.iloc[] – thisPointer

    Pandas Dataframe.iloc[] – thisPointer Skip to content In this article, we will discuss how to use the iloc property of Dataframe with examples. In Pandas, th...

  • 12

    This article will discuss how to convert Pandas Dataframe to Numpy Array. Table of Contents A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.Data...

  • 8

    This article will discuss how to convert Numpy arrays to a Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using panda...

  • 5

    Pretty Print a Pandas Dataframe – thisPointer Skip to content In this article we will discuss how to print the a Dataframe in pretty formats.

  • 9

    This article will discuss how to convert JSON to pandas Dataframe. JSON stands for JavaScript Object Notation that stores the data in key-value pair format, inside the list/dictionary data structure. A DataFrame is a data structure tha...

  • 9

    Export Pandas Dataframe to JSON – thisPointer Skip to content In this article, we will discuss how to export a Pandas Dataframe to JSON file in Python.

  • 10

    Drop Duplicate Rows from Pandas Dataframe – thisPointer In this article, we will discuss different ways to delete duplicate rows in a pandas DataFrame. Table of Contents: A DataFrame is a data structure...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK