分割数据帧的两列时出现值错误。(.all()和.any()也不起作用)

2024-05-13 09:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是数据帧信息:

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

我想要一个新的专栏,它是pdp/impression的一部分,我已经尝试了所有这些方法,但仍然得到这些错误,我该如何修复它

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()

数据帧 Data


Tags: thetonewobjectanyallnullfinal
2条回答

纯粹的尝试和错误,您是否在没有括号的情况下尝试过

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

如果它们是包含在列表中的普通整数,则应该以这种方式工作

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

但是,如果它们是numpy数组,那么您必须像

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

相关问题 更多 >