gpt4 book ai didi

python - 如何防止聚合函数删除列?

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:07 25 4
gpt4 key购买 nike

我有一个包含 3 列的数据框。我按一列进行分组,并希望聚合另一列的这些组的最大值。不过我想保留我的第三栏。 This is the dataframe I start with 。然后我按“邻居”分组并聚合“金额”的最大值。

agg_dict = {"Amount": np.max}
listings_group.groupby("neighbourhood").agg(agg_dict).reset_index()

但是this is the dataframe I end up with 。它几乎满足了我的要求,但我还想保留我的列“room_type”。

编辑

分组前的数据框

neighbourhood   room_type   Amount
0 Allerton Entire home/apt 7
1 Allerton Private room 14
2 Allerton Shared room 2
3 Arden Heights Private room 4
4 Arrochar Entire home/apt 12
5 Arrochar Private room 3
6 Arverne Entire home/apt 29
7 Arverne Private room 43
8 Arverne Shared room 2

分组后的数据框

    neighbourhood   Amount
0 Allerton 14
1 Arden Heights 4
2 Arrochar 12
3 Arverne 43
4 Astoria 458
5 Bath Beach 7
6 Battery Park City 45
7 Bay Ridge 55

最佳答案

尝试使用 groupby idxmax 进行切片上Amount

listings_group.loc[listings_group.groupby("neighbourhood")['Amount'].idxmax()]

Out[347]:
neighbourhood room_type Amount
1 Allerton Private room 14
3 Arden Heights Private room 4
4 Arrochar Entire home/apt 12
7 Arverne Private room 43
<小时/>

逐步:

groupby idxmax在专栏Amount将返回值为 Amount 的行索引每组最多。

m = listings_group.groupby("neighbourhood")['Amount'].idxmax()

Out[348]:
neighbourhood
Allerton 1
Arden Heights 3
Arrochar 4
Arverne 7
Name: Amount, dtype: int64

.locm将切片并仅返回索引值等于 m 中的值的行

listings_group.loc[m]

Out[352]:
neighbourhood room_type Amount
1 Allerton Private room 14
3 Arden Heights Private room 4
4 Arrochar Entire home/apt 12
7 Arverne Private room 43

关于python - 如何防止聚合函数删除列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58382730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com