Pandas DataFrame DataFrame.aggregate() function
The pandas.DataFrame.aggregate() function DataFrame
aggregates columns or rows of a . The most commonly used aggregation functions are min
, , max
and sum
. The result of these aggregation functions is a reduced DataFrame
size of .
pandas.DataFrame.aggregate()
grammar
DataFrame.aggregate(func, axis, *args, **kwargs)
parameter
func |
It is the aggregation function to be applied. It can be a callable or a list of callables, a string or a list of strings, or a dictionary |
axis |
The default is 0. If it is 0 or 'index' , the function is applied to each column. If it is 1 or 'column' , the function is applied to each row . |
*args |
This is a positional parameter |
**kwargs |
This is a keyword parameter |
Return Value
This function returns a scalar, Series
or DataFrame
.
- If
Series.aggressive()
a function is called with , it returns ascalar
. - If
DataFrame.agg()
a function is called with , it returns aSeries
. - If multiple functions are called
DataFrame.agg()
, it returns oneDataFrame
.
Example Code: PandasDataFrame.aggregate()
DataFrame.agg()
is DataFrame.aggregate()
an alias for . It is better to use the alias for brevity. So we will use it in the example code DataFrame.agg()
.
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print(dataframe)
Below is DataFrame
an example of .
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
Let's first examine DataFrame.agg()
the function with just one aggregate function.
import pandas as pd
dataframe = pd.DataFrame(
{
"Attendance": {0: 60, 1: 100, 2: 80, 3: 78, 4: 95},
"Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
"Obtained Marks": {0: 90, 1: 75, 2: 82, 3: 64, 4: 45},
}
)
dataframe1 = dataframe.agg("sum")
print(dataframe1)
Output:
Attendance 413
Name OliviaJohnLauraBenKevin
Obtained Marks 356
dtype: object
Aggregate functions sum
are applied to individual columns.
For integer columns, it generates the sum; for string columns, it concatenates the strings. dtype: object
The function returns Series
.
Example code: DataFrame.aggregate()
Relationship with multiple functions
import pandas as pd
dataframe = pd.DataFrame(
{
"Attendance": {0: 60, 1: 100, 2: 80, 3: 78, 4: 95},
"Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
"Obtained Marks": {0: 90, 1: 75, 2: 82, 3: 64, 4: 45},
}
)
dataframe1 = dataframe.agg(["sum", "min"])
print(dataframe1)
Output:
Attendance Name Obtained Marks
sum 413 OliviaJohnLauraBenKevin 356
min 60 Ben 45
Aggregate functions sum
and min
are applied to individual columns.
For columns of integer type, min
the function generates the minimum value, and for columns of string type, it displays the string with the minimum length.
Example code: DataFrame.aggregate()
Aggregation with specified columns
import pandas as pd
dataframe = pd.DataFrame(
{
"Attendance": {0: 60, 1: 100, 2: 80, 3: 78, 4: 95},
"Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
"Obtained Marks": {0: 90, 1: 75, 2: 82, 3: 64, 4: 45},
}
)
dataframe1 = dataframe.agg({"Obtained Marks": "sum"})
print(dataframe1)
Output:
Obtained Marks 356
dtype: int64
Returns the sum of a single column. dtype: int64
Indicates that the function returns one Series
.
We can also apply multiple functions on a column.
import pandas as pd
dataframe = pd.DataFrame(
{
"Attendance": {0: 60, 1: 100, 2: 80, 3: 78, 4: 95},
"Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
"Obtained Marks": {0: 90, 1: 75, 2: 82, 3: 64, 4: 45},
}
)
dataframe1 = dataframe.agg({"Obtained Marks": ["sum", "max"]})
print(dataframe1)
Output:
Obtained Marks
sum 356
max 90
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:197 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:191 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi
Dropping columns by index in Pandas DataFrame
Publish Date:2025/05/01 Views:99 Category:Python
-
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:
Pandas DataFrame.ix[] Function
Publish Date:2025/05/01 Views:168 Category:Python
-
Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice
Pandas DataFrame.describe() Function
Publish Date:2025/05/01 Views:120 Category:Python
-
Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a