Pandas多列分组聚合时保留同区域一致的字符串列方法
问题
现有如下Pandas DataFrame:
import pandas as pd df = pd.DataFrame([ {"area": "downtown", "type": "studio", "price": 800, "date":"2022/01/01", "area_description": "核心商业区"}, {"area": "downtown", "type": "1 bedroom", "price": 1000, "date":"2022/02/01", "area_description": "核心商业区"}, {"area": "downtown", "type": "1 bedroom", "price": 1000, "date":"2022/03/01", "area_description": "核心商业区"}, {"area": "tomato town", "type": "1 bedroom", "price": 1000, "date":"2022/04/01", "area_description": "近郊居住区"}, {"area": "tomato town", "type": "2 bedroom", "price": 2000, "date":"2022/05/01", "area_description": "近郊居住区"}, {"area": "tomato town", "type": "shop", "price": 5000, "date":"2022/06/01", "area_description": "近郊居住区"}, ])
原代码按area和type分组聚合:
stats = { 'price': ['mean', 'min', 'max'], 'date': ['max'], 'area_description': ['max'], # 原用max,但字符串用max语义不合适 } stats = df[["area", "type", "price", "date", "area_description"]].groupby(["area", "type"]).aggregate(stats)
由于area_description在同一area下取值完全一致,希望在聚合结果中保留该列,但不想用max/min这类语义不符的聚合函数,是否可以让Pandas直接保留该列值?
解决方案
当然可以,以下几种方法更贴合需求:
方法1:使用first()/last()聚合函数
因为同组内area_description值完全一致,取组内第一个或最后一个值就能得到正确结果,语义上比max/min更清晰:
stats = { 'price': ['mean', 'min', 'max'], 'date': ['max'], 'area_description': ['first'] # 用first替代max,语义更准确 } stats = df.groupby(["area", "type"]).aggregate(stats)
方法2:提前映射+合并(更高效)
如果数据集较大,先提取area与area_description的唯一映射关系,再和聚合结果合并,避免在聚合阶段处理该列,效率更高:
# 提取area到description的唯一映射 area_desc_map = df[["area", "area_description"]].drop_duplicates().set_index("area") # 聚合时只处理需要统计的列 stats = df.groupby(["area", "type"]).agg({ 'price': ['mean', 'min', 'max'], 'date': ['max'] }) # 合并映射,添加area_description列 stats = stats.join(area_desc_map, on="area")
方法3:自定义聚合取唯一值
如果想明确告知Pandas该列组内值唯一,可以用lambda函数取唯一值(前提是确认组内无重复值):
stats = { 'price': ['mean', 'min', 'max'], 'date': ['max'], 'area_description': lambda x: x.unique()[0] } stats = df.groupby(["area", "type"]).aggregate(stats)
内容的提问来源于stack exchange,提问作者Saturnix
相关产品推荐
相关产品推荐

