根据条件、Python、Pandas 查找列中所有行的最小值 [英] Finding Minimums of all rows in a column, based on criteria, Python, Pandas
问题描述
你好 Stackoverflow 社区!
Hello Stackoverflow community!
我试图找到我的问题的答案,但没有成功.我基本上是在尝试从 excel 复制 MinIF 公式,但到目前为止,我一次只能为1"行做到这一点.这是我想要实现的目标的示例:
I have attemped to find an answer to my question but without success. I am basically trying to replicate the MinIF formula from excel, but so far I have only managed to do it for "1" row at a time. Here is an example of what I am trying to achieve:
这个想法是,对于具有相同子组的所有产品,找到该组的最低价值/价格,并将其插入到它旁边的新列中.我希望我足够具体,我对 python 比较陌生,我什至不知道如何开始......
The idea is, that for all products with the same subgroup, the lowest value/price for that group is found, and inserted in a new column next to it. I hope i am being specific enough, I am relatively new to python, and I have literally no idea how to even start...
非常感谢帮助,谢谢.
推荐答案
我认为你需要 transform
:
I think you need transform
:
df['SubGroupLowestPrice'] = df.groupby('SubGroup')['Price'].transform('min')
示例:
df = pd.DataFrame({'A':[1,2,3,4,5,5],
'SubGroup':[1,5,5,6,6,6],
'Price':[7,8,9,10,2,3]})
print (df)
A Price SubGroup
0 1 7 1
1 2 8 5
2 3 9 5
3 4 10 6
4 5 2 6
5 5 3 6
df['SubGroupLowestPrice'] = df.groupby('SubGroup')['Price'].transform('min')
print (df)
A Price SubGroup SubGroupLowestPrice
0 1 7 1 7
1 2 8 5 8
2 3 9 5 8
3 4 10 6 2
4 5 2 6 2
5 5 3 6 2
这篇关于根据条件、Python、Pandas 查找列中所有行的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!