0

I was wondering the best way to essentially work out the conversion rate and place it into a conversion rate column into a pandas DataFrame.

Currently my dataframe looks like this:

Sessions    Conversions    Conversion Rate
1000        50             Default Value

I want to loop through the dataframe calculating conversion rate by doing the following code:

 e = 0
for i in dataset.itertuples():
 dataset['Conversion Rate'].loc[e] = dataset['ga:goalCompletions'].loc[e] / dataset['ga:sessions'].loc[e]
 e+=1

But I get the warning - A value is trying to be set on a copy of a slice from a DataFrame

So i'm assuming it's not the best way to do it.

Would love some help as i've been rattling my brains over this for a couple of hours now even though it's probably a super simple thing to fix...

Yarry T
  • 151
  • 3
  • 15
  • 1
    No need for loop. ALWAYS avoid `for` loops when using pandas, unless strictly necessary. In your case, just do `dataset['Conversion Rate'] = dataset['ga:goalCompletions']/dataset['ga:sessions']` – rafaelc Mar 21 '19 at 16:33
  • Possible duplicate of [Make new column in Panda dataframe by adding values from other columns](https://stackoverflow.com/questions/34023918/make-new-column-in-panda-dataframe-by-adding-values-from-other-columns) (I know you're not summing, but the concept is exactly the same) – wpercy Mar 21 '19 at 16:33
  • @RafaelC I tried removing the for loop and it worked a treat, I never knew pandas would automatically just do every row! Many thanks for your help. – Yarry T Mar 21 '19 at 16:35
  • @YarryT that's one of the main benefit of pandas. take a look at [this link](https://engineering.upside.com/a-beginners-guide-to-optimizing-pandas-code-for-speed-c09ef2c6a4d6) if you want some more context – wpercy Mar 21 '19 at 18:52

0 Answers0