Kundan
Kundan

Reputation: 590

Value Error while dividing two columns of a dataframe. (.all() and .any() are also not working)

This is the dataframe info:

new_final.info() 

<class 'pandas.core.frame.DataFrame'>
Int64Index: 12 entries, 1 to 13
Data columns (total 9 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   DorW             12 non-null     object
 1   date             12 non-null     object
 2   source           12 non-null     object
 3   AU               12 non-null     int32 
 4   impressions      12 non-null     int32 
 5   clicks           12 non-null     int32 
 6   pdp              12 non-null     int32 
 7   CABN             12 non-null     int32 
 8   order_completed  12 non-null     int32 
dtypes: int32(6), object(3)
memory usage: 672.0+ bytes

I want a new column which is the division of pdp/impression, I have tried all these methods but still get these errors how do I fix it?

new_final['impression_to_pdp'] = (new_final['pdp']/new_final['impressions']) 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

new_final['impression_to_pdp'] = (new_final['pdp']/new_final['impressions']).any() 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

new_final['impression_to_pdp'] = (new_final['pdp']/new_final['impressions']).all() 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

DataFrame Data

Upvotes: 0

Views: 312

Answers (2)

Tanuj T.V.S
Tanuj T.V.S

Reputation: 36

If they are normal integers contained in lists, it should work in this way

new_final['impression_to_pdp'] = new_final['pdp']/new_final['impressions']

but, if they are numpy arrays then you must do like

import numpy as np
new_final['impression_to_pdp'] = np.divide(new_final['pdp'].values, new_final['impressions'].values)

Upvotes: 1

gtomer
gtomer

Reputation: 6574

Pure trial and error, did you try without the brackets?:

new_final['impression_to_pdp'] = new_final['pdp']/new_final['impressions']

Upvotes: 0

Related Questions