如何将函数应用于两列以创建第三列



>我有两列,我想通过应用函数使用这两列创建第三列

# suppose I have a data frame of two columns
df =      std             mean
1.555264e+06      155980.767761
1.925751e+06      237683.694682
4.027044e+05      46319.631557
# from these two columns I want to create a column called cov 
# which will calculate the coefficient of varriation for that I defined a funtion
def cov(std, mean):
return (std/mean)*100
df['Cov'] = df.apply(lambda x: cov(x.Std, x.mean), axis=1) 
# but here the problem I face that it calculates the all variation at once and show same in every row

我尝试搜索解决方案以将函数应用于两列 Pandas 数据帧 但是我从解决方案中得到错误,例如("'numpy.float64' object has no attribute 'values'", 'occurred at index 1')

你不需要用apply,只需使用:

df['Cov'] = (df['std'] / df['mean']) * 100

最新更新