- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does agg() method do in pandas series?
The agg() method in pandas Series is used to apply one or more functions on a series object. By using this agg() method we can apply multiple functions at a time on a series.
To use multiple functions at once we need to send those function names as a list of elements to the agg() function.
Example
# import pandas package import pandas as pd # create a pandas series s = pd.Series([1,2,3,4,5,6,7,8,9,10]) print(s) # Applying agg function result = s.agg([max, min, len]) print('Output of agg method',result)
Explanation
The object “s” has 10 integer elements, and by using the agg() method we applied some aggregation operations on this series object “s”. The aggregation operations are min, max, and len.
Output
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 dtype: int64 Output of agg method max 10 min 1 len 10 dtype: int64
In this following example, The pandas series agg() method will return a series with results of each function in the list. Hence the output will be like, function name followed by the resultant output value.
Example
# import pandas package import pandas as pd # create a pandas series s = pd.Series([1,2,3,4,5,6,7,8,9,10]) print(s) # Applying agg function result = s.agg(mul) print('Output of agg method',result)
Explanation
Let’s take another example and apply a single function to the series object by using the agg() method. Here we have applied the mul function name as a parameter to the agg() function.
Output
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 dtype: int64 Output of agg method 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 dtype: int64
The output of the arr() method is displayed in the above block along with the actual series object “s”. This mul function is applied on the series elements and the resultant output is returned as another series object from the agg() method.