Python 按数据子集求列的最大值

Python 按数据子集求列的最大值,python,sql,r,sas,max,dplyr,Python,Sql,R,Sas,Max,Dplyr,我有下面的数据集,并希望找到最大的子集 数据集 我希望结果如下: 结果 基本上,每当指示符从n变为y时,我需要将StEntDD和IndicatorID作为一个块来考虑,并计算该块的最大值,并继续进行下一次迭代。p> 在R中使用的解决方案 资料 使用plyr包中的ddply的解决方案 假设它存储为一个称为df的数据帧 rep的times参数表示重复第一个参数的每个元素的次数。使用table函数计算每个学生ID出现的次数。只有在学生ID正确的情况下,这才有效。如果情况并非如此,则需要采用不同的方法

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

我希望结果如下: 结果

基本上,每当指示符从n变为y时,我需要将StEntDD和IndicatorID作为一个块来考虑,并计算该块的最大值,并继续进行下一次迭代。p> 在R中使用的解决方案

资料

使用plyr包中的ddply的解决方案

假设它存储为一个称为df的数据帧

rep的times参数表示重复第一个参数的每个元素的次数。使用table函数计算每个学生ID出现的次数。只有在学生ID正确的情况下,这才有效。如果情况并非如此,则需要采用不同的方法

总共有3行代码

output_df <- plyr::ddply(df, c("StudentID", "Indicator"), numcolwise(max))
names(output_df)[3] <- "Max"
df$Max <- rep(output_df$Max, times = table(df$StudentID))

缺少一个用于指示组的变量。您可以在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子句来删除它们。

这里有一个使用python中pandas的选项。我们通过获取逻辑输出dat.Indicator==Y的累积和来创建一个分组变量,然后通过删除“Indicator”为Y的行、按“StudentID”、“Group”分组来子集行,通过transform获得“Value”的最大值,将其分配给“Value”,并删除不需要的列

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
-输出

基本R选项将是ave

数据
你在这里实际使用哪种语言?语言在这里并不重要。我对SQL或R或Python QQ都很好:如果我必须考虑指示器Y,但是当从N切换到Y时,在第一个块中考虑对应于Y的值。@ VIAY添加突变组=IFEL指示符%Y,组1,第一个突变调用之后的组。
df$Max <- rep(output_df$Max, times = table(df$StudentID))
output_df <- plyr::ddply(df, c("StudentID", "Indicator"), numcolwise(max))
names(output_df)[3] <- "Max"
df$Max <- rep(output_df$Max, times = table(df$StudentID))
data grouped ;
  retain group 0;
  set have ;
  by studentid indicator notsorted;
  group + first.indicator;
run;
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
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
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")