[英]pandas most efficient way to execute arithmetic operations on multiple dataframe columns
my first post!我的第一篇文章! I'm running python 3.8.5 & pandas 1.1.0 on jupyter notebooks.
我在 jupyter 笔记本上运行 python 3.8.5 & pandas 1.1.0。
I want to divide several columns by the corresponding elements in another column of the same dataframe.我想用同一数据帧的另一列中的相应元素划分几列。 For example:
例如:
import pandas as pd
df = pd.DataFrame({'a': [2, 3, 4], 'b': [4, 6, 8], 'c':[6, 9, 12]})
df
a b c
0 2 4 6
1 3 6 9
2 4 8 12
I'd like to divide columns 'b' & 'c' by the corresponding values in 'a' and substitute the values in 'b' and 'c' with the result of this division.我想将 'b' 和 'c' 列除以 'a' 中的相应值,并用该除法的结果替换 'b' 和 'c' 中的值。 So the above dataframe becomes:
所以上面的数据框变成:
a b c
0 2 2 3
1 3 2 3
2 4 2 3
I tried我试过
df.iloc[: , 1:] = df.iloc[: , 1:] / df['a']
but this gives:但这给出了:
a b c
0 2 NaN NaN
1 3 NaN NaN
2 4 NaN NaN
I got it working by doing:我通过以下方式让它工作:
for colname in df.columns[1:]:
df[colname] = (df[colname] / df['a'])
Is there a faster way of doing the above by avoiding the for loop?是否有通过避免 for 循环来执行上述操作的更快方法?
thanks, mk谢谢,马克
差不多了,使用div和axis=0
:
df.iloc[:,1:] = df.iloc[:,1:].div(df.a, axis=0)
df.b= df.b/df.a
df.c=df.c/df.a
or或者
df[['b','c']]=df.apply(lambda x: x[['b','c']]/x.a ,axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.