# Pandas groupby for zero values

Given a pandas dataframe, we have to group by values and observe the frequency of zero values.
Submitted by Pranit Sharma, on October 19, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

## Problem statement

We are given a DataFrame of some products containing columns like id, name, and mfg.

## Grouping by values and observe the frequency of zero values

For this purpose, we need to use groupby() on 'id' and 'mfg' and we need to apply the count() method. Once the grouped values have their respective values, all the other values must be filled with zeroes, we need to study these zero values from groupby(). The groupby() method is a simple but very useful concept in pandas. By using groupby, we can create a grouping of certain values and perform some operations on those values.

The groupby() method splits the object, applies some operations, and then combines them to create a group hence large amounts of data and computations can be performed on these groups.

Let us understand with the help of an example,

## Python code to groupby for zero values

```# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
'id':[1,1,2,2,3],
'name':['x','x','y','y','z'],
'mfg':['14-08-2021','14-08-2021','23-12-2021','23-12-2021','10-10-2022']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original Dataframe:\n",df,"\n")

# grouping the values
res = df.groupby(['id','mfg']).count().unstack(fill_value=0).stack()

# Display Result
print('Result:\n',res)
```

## Output

The output of the above program is: