其他
25 个例子学会 Pandas Groupby 操作!
↓推荐关注↓
来源:DeepHub IMBA
import pandas as pd
sales = pd.read_csv("sales_data.csv")
sales.head()
sales.groupby("store")["stock_qty"].mean()
#输出
store
Daisy 1811.861702
Rose 1677.680000
Violet 14622.406061
Name: stock_qty, dtype: float64
2、多列聚合
sales.groupby("store")[["stock_qty","price"]].mean()sales.groupby("store")[["stock_qty","price"]].mean()
sales.groupby("store")["stock_qty"].agg(["mean", "max"])
sales.groupby("store").agg(
avg_stock_qty = ("stock_qty", "mean"),
max_stock_qty = ("stock_qty", "max")
)
sales.groupby("store")[["stock_qty","price"]].agg(["mean", "max"])
sales.groupby("store").agg(
avg_stock_qty = ("stock_qty", "mean"),
avg_price = ("price", "mean")
)
sales.groupby("store", as_index=False).agg(
avg_stock_qty = ("stock_qty", "mean"),
avg_price = ("price", "mean")
)
sales.groupby(["store","product_group"], as_index=False).agg(
avg_sales = ("last_week_sales", "mean")
).head()
sales.groupby(["store","product_group"], as_index=False).agg( avg_sales = ("last_week_sales", "mean")
).sort_values(by="avg_sales", ascending=False).head()
sales.groupby("store")["last_week_sales"].nlargest(2)
store
Daisy 413 1883
231 947
Rose 948 883
263 623
Violet 991 3222
339 2690
Name: last_week_sales, dtype: int64
11、最小的Top N
sales.groupby("store")["last_week_sales"].nsmallest(2)
12、第n个值
sales_sorted = sales.sort_values(by=["store","last_month_sales"], ascending=False, ignore_index=True)
找到每个店铺上个月销售排名第五的产品如下:
sales_sorted.groupby("store").nth(4)
sales_sorted.groupby("store").nth(-2)
sales.groupby("store", as_index=False).agg(
unique_values = ("product_code","unique")
)
sales.groupby("store", as_index=False).agg(
number_of_unique_values = ("product_code","nunique")
)
sales.groupby("store").agg(
total_sales_in_thousands = (
"last_month_sales",
lambda x: round(x.sum() / 1000, 1)
)
)
sales.groupby("store").apply(
lambda x: (x.last_week_sales - x.last_month_sales / 4).mean()
)
store
Daisy 5.094149
Rose 5.326250
Violet 8.965152
dtype: float64
18、dropna
sales.loc[1000] = [None, "PG2", 10000, 120, 64, 96, 15, 53]
sales.groupby("store")["price"].mean()
store
Daisy 69.327426
Rose 60.513700
Violet 67.808727
Name: price, dtype: float64
sales.groupby("store", dropna=False)["price"].mean()
store
Daisy 69.327426
Rose 60.513700
Violet 67.808727
NaN 96.000000
Name: price, dtype: float64
sales.groupby(["store", "product_group"]).ngroups
18
aisy_pg1 = sales.groupby(
["store", "product_group"]).get_group(("Daisy","PG1")
)
daisy_pg1.head()
sales["rank"] = sales.groupby("store"["price"].rank(
ascending=False, method="dense"
)
sales.head()
import numpy as npdf = pd.DataFrame(
{
"date": pd.date_range(start="2022-08-01", periods=8, freq="D"),
"category": list("AAAABBBB"),
"value": np.random.randint(10, 30, size=8)
}
)
df["cum_sum"] = df.groupby("category")["value"].cumsum()
df["cum_sum_2"] = df.groupby(
"category"
)["value"].expanding().sum().values
df["cum_mean"] = df.groupby(
"category"
)["value"].expanding().mean().values
df["current_highest"] = df.groupby(
"category"
)["value"].expanding().max().values
- EOF -
加主页君微信,不仅数据分析和开发技能+1
主页君日常还会在个人微信分享数据分析和开发相关工具、资源和精选技术文章,不定期分享一些有意思的活动、岗位内推以及如何用技术做业余项目
加个微信,打开一扇窗
看完本文有收获?请转发分享给更多人
点赞和在看就是最大的支持❤️