这是示例示例:
input:
sub del wait
12 55 11
13 33 71
例如: 第一行: del = 55 * 100/12 = 458.33 等待= 11 * 100/12 = 91.66
第二行: del = 33 * 100/13 = 253.3 等待= 71 * 100/13 = 546.01
最终输出应为:
sub del wait
12 458.33 91.66
13 253.3 546.01
答案 0 :(得分:2)
选择两个列,分别乘以100
和除以DataFrame.div
,然后在必要时用DataFrame.round
分配列:
df[['del','wait']] = df[['del','wait']].mul(100).div(df['sub'], axis=0).round(2)
print (df)
sub del wait
0 12 458.33 91.67
1 13 253.85 546.15
或者用Series.to_numpy
创建的形状(N x 1)的numpy数组除以每行的除法:
df[['del','wait']] = (df[['del','wait']] * 100 / df['sub'].to_numpy()[:, None]).round(2)
print (df)
sub del wait
0 12 458.333333 91.666667
1 13 253.846154 546.153846