并提前感谢您的帮助。
我希望通过将现有列的子集除以另一个现有列(用后缀动态命名)来在 pandas 数据框中创建多个新列。下面是虚拟代码,说明了我想要做的事情的一般要点,除了具有各种转换的 25+ 列之外。
R代码
library(dplyr)
player = c('John','Peter','Michael')
min = c(20, 23, 35)
points = c(10,12,14)
rebounds = c(5,7,9)
assists = c(4,6,7)
df = data.frame(player,min,points,rebounds,assists)
df = df %>%
mutate_at(vars(points:assists),.funs=funs(per_min=./min))
Run Code Online (Sandbox Code Playgroud)
预期产出
player min points rebounds assists points_per_min rebounds_per_min assists_per_min
1 John 20 10 5 4 0.5000000 0.2500000 0.2000000
2 Peter 23 12 7 6 0.5217391 0.3043478 0.2608696
3 Michael 35 14 9 7 0.4000000 0.2571429 0.2000000
Run Code Online (Sandbox Code Playgroud)
我知道我可以在 pandas 中重现上述内容,如下所示:
import pandas as pd
data = pd.DataFrame({'player':['John','Peter','Michael'],
'min':[20,23,35], …Run Code Online (Sandbox Code Playgroud)