简体   繁体   English

使用其他行中的非唯一值从 Dataframe 行中提取值

[英]extracting values from a Dataframe rows using non unique values in other rows

Given the following Dataframe,鉴于以下 Dataframe,

  SecondsInDay  Min  Max
0             0    1    2
1           300    3    4
2           600    5    6
3             0    7    8
4           300    1    0
5           300    2   12
6           300    4   56

I want to extract overall minimum and maximum value for every row where SecondsInDay = 300.我想提取 SecondsInDay = 300 的每一行的总体最小值和最大值。

Being new to DataFrames, it took a while but I wrote the following code:作为 DataFrames 的新手,花了一些时间,但我编写了以下代码:

val = df[df.SecondsInDay==300]
index=val.index
maxVal=val['Max'][index[0]]
minVal=val['Min'][index[0]]
for d in index:
    if maxVal < val['Max'][d]:
        maxVal = val['Max'][d]
    if minVal> val['Min'][d]:
        minVal= val['Min'][d]

which indeed returns the correct values它确实返回了正确的值

maxVal=56
minVal=1

However, I read that iterating through DataFrames is not good particularly at scale,但是,我读到迭代 DataFrames 并不是很好,特别是在规模上,

therefore could I have written this better?因此我能写得更好吗?

thanks in advance提前致谢

So we can do agg所以我们可以做agg

df.loc[df.SecondsInDay==300].agg({'Min':'min','Max':'max'})
Out[122]: 
Min     1
Max    56
dtype: int64

IICU重症监护病房

df[df['SecondsInDay']==300].agg(['max','min'] )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM