小编use*_*328的帖子

在Python Pandas中,如何使用像R dplyr mutate_each

在Python Pandas中,我想通过在多个列上执行多个聚合函数来添加列,如R dplyr mutate_each.例如,Python Pandas可以实现与以下R脚本相同的处理吗?

R dplyr :

 iris %>%
   group_by(Species) %>%
   mutate_each(funs(min, max, mean), starts_with("Sepal"))
Run Code Online (Sandbox Code Playgroud)

但是,我能够实现与Pandas mutate相同的处理.如下面的代码所示,我可以执行一个聚合函数并添加一列.

R dplyr :

 iris %>% group_by(Species) %>% mutate(MaxSepalLen = max(Sepal.Length))

Python Pandas :

 iris.assign(MaxSepalLen = iris.groupby("Species")["Sepal.Length"].transform('max'))
Run Code Online (Sandbox Code Playgroud)

python r pandas dplyr

5
推荐指数
1
解决办法
542
查看次数

标签 统计

dplyr ×1

pandas ×1

python ×1

r ×1