Pandas GroupBy - Count last value
Last Updated :
23 Sep, 2022
A groupby operation involves grouping large amounts of data and computing operations on these groups. It is generally involved in some combination of splitting the object, applying a function, and combining the results. In this article let us see how to get the count of the last value in the group using pandas.
Syntax:
DataFrame.groupby(by, axis, as_index)
Parameters:
- by (datatype- list, tuples, dict, series, array): mapping, function, label, or list of labels. The function passed is used as-is to determine the groups.
- axis (datatype int, default 0): 1 - splits columns and 0 - splits rows.
- as_index (datatype bool, default True.): Returns an object with group labels as the index, for all aggregated output,
Method 1: Using GroupBy & Aggregate function
In this approach, the user needs to call the DataFrame.groupby() function to demonstrate how to get the count of the last value in the group using pandas in the python language.
Example:
In this example, we create a sample dataframe with car names and prices as shown and apply groupby function on cars, setting as_index false doesn't create a new index then aggregate the grouped function by the last price of the cars using the 'last' parameter in the aggregate function and name the column 'Price_last'.Followed by that add another lambda function to get the number of times the car got the last price.
The dataframe used in the below example:
cars Price_in_million
0 benz 15
1 benz 12
2 benz 23
3 benz 23
4 bmw 63
5 bmw 34
6 bmw 63
Python3
# import python pandas package
import pandas as pd
# create a sample dataframe
data = pd.DataFrame({'cars': ['benz', 'benz', 'benz',
'benz', 'bmw', 'bmw', 'bmw'],
'Price_in_million': [15, 12, 23, 23,
63, 34, 63]})
# use groupby function to groupby cars, setting
# as_index false doesnt create an index.
# use aggregate function with 'last; parameter
# to get the last price in the group of cars.
# apply lambda function to get the number of
# times the car got the last price.
data.groupby('cars', as_index=False).agg(Price_last=('Price_in_million', 'last'),
Price_last_count=('Price_in_million',
lambda x: sum(x == x.iloc[-1])))
Output:

In this method, the user has to call the lambda function used above to return, the count of the corresponding rows present in the dataframe in the R programming language.
Example:
Under this example, as you can see, the car - BMW and the price 63 corresponds to the 4th row in the dataset. Applying the lambda function as above returns that the car BMW is present three times and the price 63 is present 2 times.
Python3
# import python pandas package
import pandas as pd
# create a sample dataframe
data = pd.DataFrame({'cars': ['benz', 'benz', 'benz',
'benz', 'bmw', 'bmw', 'bmw'],
'Price_in_million': [15, 12, 23, 23, 63, 34, 63]})
# get the 4th row present in the data
data.iloc[4]
# Now apply lambda function to get the number
# of times the row is present in the dataset
data.apply(lambda x: sum(x==x.iloc[4]))
Output:

Method 3: Using GroupBy, pandas Merge & Aggregate function
 The count of the last value in the group using pandas can also be got using the pandas merge function as follows.
Syntax:
DataFrame.merge(right, how='inner', on=None)
Parameters:
- right - Object to merge with. (dataframe or series object).
- how - left join, right join, outer join, Default - Inner join
- on - (label or list). Â Specify the Column names to join on.
Example:
In this example, we create a sample dataframe with car names and prices as shown and apply groupby function on cars, and use tail() function to compute the final value of the group. Now, perform an inner merge with the grouped dataset and the original dataset. Finally, apply a count aggregated groupby function to get the no. of occurrences of the last value.
Python3
# import pandas package
import pandas as pd
# create a sample dataset
data = pd.DataFrame({'cars': ['benz', 'benz', 'benz',
'benz', 'bmw', 'bmw', 'bmw'],
'Price_in_million': [15, 12, 23, 23, 63, 34, 63]})
# perform inner merge with the grouped and original dataset
merged = pd.merge(data.groupby('cars').tail(1), data, how='inner')
# apply a count aggregated groupby function to
# get the no. of. occurrences of last value.
result = merged.groupby(['cars', 'Price_in_million'])[
'Price_in_million'].agg('count')
print(result)
Output:

Method 4: Using GroupBy, pandas Merge, and Sum function
We can also get the same result by slightly altering the above approach, using the last() function instead of tail(), as shown below,
Example:
In this example, we create a sample dataframe with car names and prices as shown, apply groupby function on cars, and use the last() function to find the final element of every group and inner Merge the grouped dataset with the original dataset. Now compare the two prices in the merged columns and create a new column of bool data type, where the prices match. Now use the groupby function to get the number of times the last value of the group is repeated.
Python3
# import pandas package
import pandas as pd
# create a sample dataset
data = pd.DataFrame({'cars': ['benz', 'benz', 'benz',
'benz', 'bmw', 'bmw', 'bmw'],
'Price_in_million': [15, 12, 23, 23, 63, 34, 63]})
# computes the final value of each group
grouped = data.groupby('cars').last()
# Merge dataset named "data" with this result
data = data.merge(grouped, left_on='cars', right_index=True, how='inner')
# Now compare the merged columns for same price
# and create a new column of boolean values
# where prices match
data['count'] = data['Price_in_million_x'] == data['Price_in_million_y']
# Use groupby function to return the aggregated
# sum of count column where the price matches
data.groupby('cars')['count'].sum()
Output:

Similar Reads
Pandas - Groupby value counts on the DataFrame
Prerequisites: Pandas Pandas can be employed to count the frequency of each value in the data frame separately. Let's see how to Groupby values count on the pandas dataframe. To count Groupby values in the pandas dataframe we are going to use groupby() size() and unstack() method. Functions Used:gro
3 min read
How to List values for each Pandas group?
In this article, we'll see how we can display all the values of each group in which a dataframe is divided. The dataframe is first divided into groups using the DataFrame.groupby() method. Then we modify it such that each group contains the values in a list. First, Let's create a Dataframe: [GFGTABS
2 min read
Count unique values with Pandas per groups
Prerequisites: Pandas In this article, we are finding and counting the unique values present in the group/column with Pandas. Unique values are the distinct values that occur only once in the dataset or the first occurrences of duplicate values counted as unique values. Approach:Import the pandas li
7 min read
Pandas Groupby and Sum
It's a simple concept but it's an extremely valuable technique that's widely used in data science. It is helpful in the sense that we can : Compute summary statistics for every groupPerform group-specific transformationsDo the filtration of data The dataframe.groupby() involves a combination of spli
2 min read
How to count unique values in a Pandas Groupby object?
Here, we can count the unique values in Pandas groupby object using different methods. This article depicts how the count of unique values of some attribute in a data frame can be retrieved using Pandas. Method 1: Count unique values using nunique() The Pandas dataframe.nunique() function returns a
3 min read
Count Values in Pandas Dataframe
Counting values in Pandas dataframe is important for understanding the distribution of data, checking for missing values or summarizing data. In this article, we will learn various methods to count values in a Pandas DataFrame We will be using below dataframe to learn about various methods: [GFGTABS
3 min read
Pandas Groupby and Computing Mean
Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-perform
2 min read
How to Plot Value Counts in Pandas
In this article, we'll learn how to plot value counts using provide, which can help us quickly understand the frequency distribution of values in a dataset. Table of Content Concepts Related to Plotting Value CountsSteps to Plot Value Counts in Pandas1. Install Required Libraries2. Import Required L
3 min read
Pandas - GroupBy One Column and Get Mean, Min, and Max values
We can use Groupby function to split dataframe into groups and apply different operations on it. One of them is Aggregation. Aggregation i.e. computing statistical parameters for each group created example - mean, min, max, or sums. Let's have a look at how we can group a dataframe by one column and
2 min read
Dividing Values of Grouped Columns in Pandas
In Pandas, the groupby method is a powerful tool for aggregating and analyzing data based on specific criteria. When seeking divided values of two columns resulting from a groupby operation, you can use various techniques. In this article, we will explore three different methods/approaches to get th
4 min read