如何在Pandas中结合Groupby和多个聚合函数
Pandas是一个Python软件包,提供各种数据结构和操作,用于处理数字数据和时间序列。它主要因导入和分析数据更容易而流行。它是一个建立在NumPy库之上的开源库。
Groupby()
Pandas dataframe.groupby()函数用于根据给定的条件,将数据框架中的数据分成若干组。
示例 1:
# import library
import pandas as pd
# import csv file
df = pd.read_csv("https://bit.ly/drinksbycountry")
df.head()
输出:
示例 2:
# Find the average of each continent
# by grouping the data
# based on the "continent".
df.groupby(["continent"]).mean()
输出:
Aggregate()
Pandas dataframe.agg()函数用于根据指定的axis对数据进行一次或多次操作
示例:
# here sum, minimum and maximum of column
# beer_servings is calculatad
df.beer_servings.agg(["sum", "min", "max"])
输出:
同时使用这两个函数:我们可以找到由另一列分组的某一列的多个聚合函数。
示例:
# find an aggregation of column "beer_servings"
# by grouping the "continent" column.
df.groupby(df["continent"]).beer_servings.agg(["min",
"max",
"sum",
"count",
"mean"])
输出: