用d的子集求列的最大值

2024-10-01 09:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的数据集,希望通过子集找到最大值
数据集

StudentID Indicator Value  
100 N 30  
100 N 35  
100 N 28  
100 Y 20  
100 N 29  
100 N 60  
200 N 40  
200 N 35  
200 Y 20  
200 N 24  
200 N 35  

我希望结果如下:
结果

StudentID Indicator Value Max  
100 N 30 35  
100 N 35 35  
100 N 28 35  
100 N 29 60   
100 N 60 60  
200 N 40 40  
200 N 35 40  
200 N 24 35  
200 N 35 35  

所以本质上,每当指示符从N变为Y时,我需要将studentID和IndicatorID的行视为一个块,计算该块的最大值,然后继续下一次迭代。你知道吗


Tags: 数据valuemax子集indicator本质studentidindicatorid
3条回答

下面是一个使用pandas来自python的选项。我们通过获取逻辑输出的累积和来创建一个分组变量(dat.Indicator == "Y"),然后通过删除“Indicator”为“Y”的行来子集行,按“StudentID”、“Group”分组,用transform获取“Value”的max,将其分配给“Value”,并drop不需要的列

dat['Group'] = (dat.Indicator == "Y").cumsum()

datS = dat[dat.Indicator != "Y"]
datS1 = datS.copy()
datS1['Value'] = datS.groupby(['StudentID', 'Group'])['Value'].transform('max')
datS1.drop('Group', axis = 1, inplace = True)
datS1

-输出

enter image description here


一个base R选项应该是ave

dat$Value <- with(dat, ave(Value, cumsum(Indicator == "Y"), FUN = max))
subset(dat, Indicator != "Y")
#    StudentID Indicator Value
#1        100         N    35
#2        100         N    35
#3        100         N    35
#5        100         N    60
#6        100         N    60
#7        200         N    60
#8        200         N    60
#10       200         N    35
#11       200         N    35

数据

import pandas as pd
dat = pd.DataFrame({'StudentID': [100, 100, 100, 100, 100, 100, 200, 200, 200, 200, 200],
               'Indicator':[ "N", "N", "N", "Y", "N", "N", "N", "N", "Y", "N", "N"],
               'Value':[30, 35, 28, 20, 29, 60, 40, 35, 20, 24, 35]})

#R
dat <-structure(list(StudentID = c(100L, 100L, 100L, 100L, 100L, 100L, 
 200L, 200L, 200L, 200L, 200L), Indicator = c("N", "N", "N", "Y", 
"N", "N", "N", "N", "Y", "N", "N"), Value = c(35L, 35L, 35L, 
60L, 60L, 60L, 60L, 60L, 35L, 35L, 35L)), .Names = c("StudentID", 
 "Indicator", "Value"), row.names = c(NA, -11L), class = "data.frame")

缺少用于指示组的变量。您可以在SAS中使用by语句上的notsorted选项轻松地实现这一点。你知道吗

data grouped ;
  retain group 0;
  set have ;
  by studentid indicator notsorted;
  group + first.indicator;
run;

现在有很多方法可以根据组来生成平均值。PROC SQL通过自动将聚合值重新合并回细节行,使之变得简单。你知道吗

proc sql ;
 select *,max(value) as max
   from grouped
   group by group
 ;
quit;

结果:

group  StudentID  Indicator     Value       max

   1        100  N                35        35
   1        100  N                30        35
   1        100  N                28        35
   2        100  Y                20        20
   3        100  N                60        60
   3        100  N                29        60
   4        200  N                40        40
   4        200  N                35        40
   5        200  Y                20        20
   6        200  N                35        35
   6        200  N                24        35

我不知道为什么您的示例输出删除了INDICATOR='Y'的组,但是您可以添加一个where子句来删除它们。你知道吗

在R中使用的解

library(dplyr)

dat2 <- dat %>%
  group_by(StudentID) %>%
  mutate(Group = cumsum(Indicator %in% "Y")) %>%
  filter(!Indicator %in% "Y") %>%
  group_by(StudentID, Group) %>%
  mutate(Max = max(Value)) %>%
  ungroup() %>%
  select(-Group) %>%
  as.data.frame(stringsAsFactors = FALSE)
dat2
#   StudentID Indicator Value Max
# 1       100         N    30  35
# 2       100         N    35  35
# 3       100         N    28  35
# 4       100         N    29  60
# 5       100         N    60  60
# 6       200         N    40  40
# 7       200         N    35  40
# 8       200         N    24  35
# 9       200         N    35  35

数据

dat <- read.table(text = "StudentID Indicator Value  
100 N 30  
                  100 N 35  
                  100 N 28  
                  100 Y 20  
                  100 N 29  
                  100 N 60  
                  200 N 40  
                  200 N 35  
                  200 Y 20  
                  200 N 24  
                  200 N 35  ",
                  header = TRUE, stringsAsFactors = FALSE)

相关问题 更多 >