如果某些行的类型为字符串,则无法对列进行数学运算

时间:2018-11-21 01:28:21

标签: python pandas

这是我的df样本:

    units                      price
0   143280.0                   0.8567                        
1   4654.0                    464.912                       
2   512210.0                   607  
3  Unknown                    0                          
4  Unknown                    0 

我有以下代码:

myDf.loc[(myDf["units"].str.isnumeric())&(myDf["price"].str.isnumeric()),'newValue']=(    
myDf["price"].astype(float).fillna(0.0)*
myDf["units"].astype(float).fillna(0.0)/
1000)

如您所见,我正在尝试仅做数学运算来为两个源列均为数字的行创建'newValue'列。但是,出现以下错误:

ValueError: could not convert string to float: 'Unknown'

因此,即使我试图仅对没有文本的行执行数学运算,Pandas也不喜欢其中任何行都有文本。

请注意,我需要完全按原样维护“未知”实例,因此用零填充这些实例不是一个好的选择。

这已经很困难了。通过搜索Google找不到任何解决方案。

将感谢您的帮助/解决方案。

1 个答案:

答案 0 :(得分:1)

您可以使用与右侧=左侧相同的条件,如下所示(为便于阅读,我在变量is_num中设置了条件)

is_num = (myDf["units"].astype(str).str.replace('.', '').str.isnumeric()) & (myDf["price"].astype(str).str.replace('.', '').str.isnumeric())
myDf.loc[is_num,'newValue']=( 
       myDf.loc[is_num, "price"].astype(float).fillna(0.0)* 
       myDf.loc[is_num, "units"].astype(float).fillna(0.0)/1000)

此外,您需要检查读取的数据帧,但是从此示例中,您可以:

  1. 删除fillna(0.0),因为没有NaN
  2. 删除对“价格”的检查(例如,价格始终为数字,因此不需要检查)
  3. 删除价格的astype(float)强制转换,因为它已经是数字了。

这将导致以下更简洁的代码:

is_num = myDf["units"].astype(str).str.replace('.', '').str.isnumeric()
myDf.loc[is_num,'newValue']=( 
       myDf.loc[is_num, "price"].astype(float)* 
       myDf.loc[is_num, "units"]/1000)