我在pandas数据帧上使用otherstuff来删除所有没有特定列的最小值的行。 像这样:
df1 = df.groupby("item", as_index=False)["diff"].min()
但是,如果我不止这两列,其他列(例如,在我的示例中为otherstuff)将被删除。 我可以使用groupby保留这些列,还是必须找到一种不同的方式删除行?
我的数据如下:
item diff otherstuff
0 1 2 1
1 1 1 2
2 1 3 7
3 2 -1 0
4 2 1 3
5 2 4 9
6 2 -6 2
7 3 0 0
8 3 2 9
最终应该像:
item diff otherstuff
0 1 1 2
1 2 -6 2
2 3 0 0
但是我得到的是:
item diff
0 1 1
1 2 -6
2 3 0
我一直在浏览文档,找不到任何东西。 我试过了:
df1 = df.groupby(["item", "otherstuff"], as_index=false)["diff"].min()
df1 = df.groupby("item", as_index=false)["diff"].min()["otherstuff"]
df1 = df.groupby("item", as_index=false)["otherstuff", "diff"].min()
但是这些都不起作用(我在最后一个中意识到,语法是在创建组之后进行聚合的)。