Pandas Dataframe无效密钥


df.loc[(df['Original Price'] != '0') & (df['Old Original Price'] != '0'), 'Price Difference'].index = df['Original Price'] - df['Old Original Price']

我目前正在尝试比较数据框的两列,如果它们的值不等于0,它们将创建一个新列并在两个值之间做减法

虽然我似乎一直得到下面提到的错误:

TypeError: '(Series([], dtype: bool), 'Price Difference')' is an invalid key

您可以尝试:

df.loc[((df['Original Price'] != '0') & (df['Old Original Price'] != '0')),'Price_Difference'] = df['Original Price'] - df['Old Original Price']

我需要看到一个数据的样例,以及列的数据类型,但根据您提到的,我建议如下:

df['Price Difference']= df['Original Price']
df.loc[((~df['Original Price'].isin(['0'])) & (~df['Old Original Price'].isin(['0']))), 'Price Difference']= df['Original Price'] - df['Old Original Price']

试试这个

df[Price Difference] =df.apply(function,axis=1)
在方法 中写入逻辑
function(row):
{
if (row['Original Price'] != '0') & (row['Old Original Price'] != 
'0'):
return row['Original Price'] - row['Old Original Price']
} 

相关内容

  • 没有找到相关文章

最新更新