I have a dataframe in R. Example:
d1<-structure(list(A = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), B = 1:9), .Names = c("A",
"B"), class = "data.frame", row.names = c(NA, -9L))
我想要输出如下:
d2<-structure(list(A = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), B = 1:9,
c = c(3L, 3L, 3L, 7L, 7L, 7L, 7L, 9L, 9L)), .Names = c("A",
"B", "c"), class = "data.frame", row.names = c(NA, -9L))
我可以使用 R 中的
ave
函数来完成这个任务。d1$c<-ave(d1$B,d1$A,FUN=max)
现在我想用Python实现。我的意思是,如果我有像d1这样的数据,我该如何在Python Pandas中创建d2呢?