# Python - Count distinct in Pandas Aggregation with Numpy

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

To count distinct, use nunique in Pandas. We will groupby a column and find sun as well using Numpy sum().

At first, import the required libraries −

import pandas as pd
import numpy as np

Create a DataFrame with 3 columns. The columns have duplicate values −

dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'],"Place": ['Delhi','Bangalore','Delhi','Chandigarh','Chandigarh'],"Units": [100, 150, 50, 110, 90]
}
)

Count distinct in aggregation agg() with nunique. Calculating the sum for counting, we are using numpy sum() −

dataFrame = dataFrame.groupby("Car").agg({"Units": np.sum, "Place": pd.Series.nunique})

## Example

Following is the code −

import pandas as pd
import numpy as np

dataFrame = pd.DataFrame(
{
"Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'],"Place": ['Delhi','Bangalore','Delhi','Chandigarh','Chandigarh'],"Units": [100, 150, 50, 110, 90]
}
)

print"DataFrame ...\n",dataFrame

# count distinct in aggregation with nunique
dataFrame = dataFrame.groupby("Car").agg({"Units": np.sum, "Place": pd.Series.nunique})

print"\nUpdated DataFrame ...\n",dataFrame

## Output

This will produce the following output −

DataFrame ...
Car      Place   Units
0    BMW      Delhi    100
1   Audi  Bangalore    150
2    BMW      Delhi     50
3  Lexus Chandigarh    110
4  Lexus Chandigarh     90

Updated DataFrame ...
Units   Place
Car
Audi    150       1
BMW     150       1
Lexus   200       1
{"ft_lang_label":"__label__en","ft_lang_prob":0.64522433,"math_prob":0.80549484,"size":2862,"snap":"2022-40-2023-06","text_gpt3_token_len":732,"char_repetition_ratio":0.13610917,"word_repetition_ratio":0.13769752,"special_character_ratio":0.2683438,"punctuation_ratio":0.16194332,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98870635,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-03T15:32:04Z\",\"WARC-Record-ID\":\"<urn:uuid:6cad6c02-6cc2-4cb1-9573-1c21ae4e17ac>\",\"Content-Length\":\"37784\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d296765e-cbb5-429d-8c6c-c9c68a20ed7f>\",\"WARC-Concurrent-To\":\"<urn:uuid:d04ac8c0-dc5e-469c-bdeb-ff65cde9afcd>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/python-count-distinct-in-pandas-aggregation-with-numpy\",\"WARC-Payload-Digest\":\"sha1:X7SX3DJJNHGPC4VZIY5CBTTUZQXWIUOZ\",\"WARC-Block-Digest\":\"sha1:H24FLPLTILPCOYE2K2YKUMTCVIE4R62X\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337421.33_warc_CC-MAIN-20221003133425-20221003163425-00475.warc.gz\"}"}