Every line of 'pandas rank within group' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
235 def _ros_group_rank(df, dl_idx, censorship): 236 """ 237 Ranks each observation within the data groups. 238 239 In this case, the groups are defined by the record's detection 240 limit index and censorship status. 241 242 Parameters 243 ---------- 244 df : pandas.DataFrame 245 246 dl_idx : str 247 Name of the column in the dataframe the index of the 248 observations' corresponding detection limit in the `cohn` 249 dataframe. 250 251 censorship : str 252 Name of the column in the dataframe that indicates that a 253 observation is left-censored. (i.e., True -> censored, 254 False -> uncensored) 255 256 Returns 257 ------- 258 ranks : numpy.array 259 Array of ranks for the dataset. 260 """ 261 262 # (editted for pandas 0.14 compatibility; see commit 63f162e 263 # when `pipe` and `assign` are available) 264 ranks = df.copy() 265 ranks.loc[:, 'rank'] = 1 266 ranks = ( 267 ranks.groupby(by=[dl_idx, censorship])['rank'] 268 .transform(lambda g: g.cumsum()) 269 ) 270 return ranks