Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Pandas Series.plot() method
Next article icon

Pandas Series agg() Method

Last Updated : 23 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.agg() is used to pass a function or list of functions to be applied on a series or even each element of the series separately. In the case of a list of functions, multiple results are returned by the Series.agg() method.

Pandas Series Aggregate Syntax

Syntax: Series.agg(func, axis=0) 

Parameters: 

  • func: Function, list of function or string of function name to be called on Series. 
  • axis:0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation. 

Return Type: The return type depends on return type of function passed as parameter.

Aggregate the Series Elements in Pandas

In Pandas, series elements can be aggregated by computing statistical measures such as sum, mean, min, max, and count. These functions can provide insights into the dataset’s characteristics.

Python3

import pandas as pd
s = pd.Series([89,99,78,70])
s.agg('min')
s.agg(['min', 'max'])
                      
                       

Output:

min    70 max   99

Example 1: In this example, a Python lambda function is passed which simply adds 2 to each value of the series. Since the function will be applied to each value of the series, the return type is also a series. A random series of 10 elements is generated by passing an array generated using Numpy random method.

Python3

# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# creating random arr of 10 elements
arr = np.random.randn(10)
 
# creating series from array
series = pd.Series(arr)
 
# calling .agg() method
result = series.agg(lambda num: num + 2)
 
# display
print('Array before operation: \n', series,
      '\n\nArray after operation: \n', result)
                      
                       

Output: 

As shown in the output, the function was applied to each value and 2 was added to each value of the series.

Array before operation:   0   -0.178400 1   -0.014408 2   -2.185778 3    0.335517 4    1.013446 5    0.897206 6    0.116324 7   -1.046006 8   -0.918818 9    0.552542 dtype: float64  Array after operation:   0    1.821600 1    1.985592 2   -0.185778 3    2.335517 4    3.013446 5    2.897206 6    2.116324 7    0.953994 8    1.081182 9    2.552542 dtype: float64 

Example 2: Passing List of functions In this example, a list of some of Python’s default functions is passed and multiple results are returned by Pandas Series.agg() method into multiple variables. 

Python3

# importing pandas module
import pandas as pd
 
# importing numpy module
import numpy as np
 
# creating random arr of 10 elements
arr = np.random.randn(10)
 
# creating series from array
series = pd.Series(arr)
 
# creating list of function names
func_list = [min, max, sorted]
 
# calling .agg() method
# passing list of functions
result1, result2, result3 = series.agg(func_list)
 
# display
print('Series before operation: \n', series)
print('\nMin = {}\n\nMax = {},\
      \n\nSorted Series:\n{}'.format(result1, result2, result3))
                      
                       

Output: 

As shown in the output, multiple results were returned. Min, Max, and Sorted array were returned into different variables result1, result2, and result3 respectively.

Series before operation:   0   -1.493851 1   -0.658618 2    0.265253 3   -0.503875 4    1.419182 5    0.221025 6   -0.712019 7   -1.462868 8   -0.341504 9   -0.338337 dtype: float64 Min = -1.4938513079840412 Max = 1.4191824761086351,       Sorted Series: [-1.4938513079840412, -1.462868259420631, -0.7120191767162937, -0.6586184541010776,  -0.5038754446324809, -0.34150351227216663, -0.33833663286234356,  0.22102480822109685, 0.2652526809574672, 1.4191824761086351] 


Next Article
Python | Pandas Series.plot() method

K

Kartikaybhutani
Improve
Article Tags :
  • Python
  • Technical Scripter
  • Python pandas-series
Practice Tags :
  • python

Similar Reads

  • Creating a Pandas Series
    A Pandas Series is like a single column of data in a spreadsheet. It is a one-dimensional array that can hold many types of data such as numbers, words or even other Python objects. Each value in a Series is associated with an index, which makes data retrieval and manipulation easy. This article exp
    3 min read
  • Python | Pandas Series.get()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.get() function get item from
    2 min read
  • Python | Pandas Series.mod()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Python Series.mod() is used to return the remainder after division of two numbers Synt
    3 min read
  • Python | Pandas Series.plot() method
    With the help of Series.plot() method, we can get the plot of pandas series by using Series.plot() method. Syntax : Series.plot() Return : Return the plot of series. Example #1 : In this example we can see that by using Series.plot() method, we are able to get the plot of pandas series. # import Ser
    1 min read
  • Python | Pandas Series.aggregate()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.aggregate() function aggregat
    3 min read
  • Python | Pandas Series.str.isspace() method
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analysing data much easier. Pandas isspace() is a string method, it checks for All-Space characters in a series an
    3 min read
  • Accessing elements of a Pandas Series
    Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. An element in the series can be accessed similarly to that in an ndarray. Elements of a series can be accessed i
    6 min read
  • Python | Pandas Series.mode()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.mode() function return the mo
    2 min read
  • Python | Pandas Series.ge()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series.ge() is used to compare every element of Caller series with passed serie
    3 min read
  • Python | Pandas Series.at
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.at attribute enables us to access a single value for a row/column label
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences