2

I have two columns and I want to create the third column using that two columns by applying a function

# 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

I tried to search solution to apply a function to two columns of Pandas dataframe but I am getting error from the solution such as ("'numpy.float64' object has no attribute 'values'", 'occurred at index 1')

Community
  • 1
  • 1
Darkstar Dream
  • 1,649
  • 1
  • 12
  • 23

1 Answers1

2

You don't need to use apply, just use:

df['Cov'] = (df['std'] / df['mean']) * 100
U13-Forward
  • 69,221
  • 14
  • 89
  • 114