简体   繁体   English

pandas表子集给出无效的类型比较错误

[英]pandas table subsets giving invalid type comparison error

I am using pandas and want to select subsets of data and apply it to other columns. 我正在使用pandas并希望选择数据子集并将其应用于其他列。 eg 例如

  • if there is data in column A; 如果A栏中有数据; &
  • if there is NO data in column B; 如果B栏中没有数据;
  • then, apply the data in column A to column D 然后,将A列中的数据应用于D列

I have this working fine for now using .isnull() and .notnull() . 我现在使用.isnull().notnull() eg 例如

df = pd.DataFrame({'A' : pd.Series(np.random.randn(4)),
                       'B' : pd.Series(np.nan),
                       'C' : pd.Series(['yes','yes','no','maybe'])})
df['D']=''

df
Out[44]: 
          A   B      C D
0  0.516752 NaN    yes  
1 -0.513194 NaN    yes  
2  0.861617 NaN     no  
3 -0.026287 NaN  maybe  

# Now try the first conditional expression
df['D'][df['A'].notnull() & df['B'].isnull()] \
=  df['A'][df['A'].notnull() & df['B'].isnull()]   
df
Out[46]: 
          A   B      C          D
0  0.516752 NaN    yes   0.516752
1 -0.513194 NaN    yes  -0.513194
2  0.861617 NaN     no   0.861617
3 -0.026287 NaN  maybe -0.0262874

When one adds a third condition, to also check whether data in column C matches a particular string, we get the error: 当添加第三个条件时,还要检查C列中的数据是否与特定字符串匹配,我们得到错误:

df['D'][df['A'].notnull() & df['B'].isnull() & df['C']=='yes'] \
=  df['A'][df['A'].notnull() & df['B'].isnull() & df['C']=='yes']   


  File "C:\Anaconda2\Lib\site-packages\pandas\core\ops.py", line 763, in wrapper
    res = na_op(values, other)

  File "C:\Anaconda2\Lib\site-packages\pandas\core\ops.py", line 718, in na_op
    raise TypeError("invalid type comparison")

TypeError: invalid type comparison

I have read that this occurs due to the different datatypes. 我已经读过这是因为数据类型不同而发生的。 And I can get it working if I change all the strings in column C for integers or booleans. 如果我在C列中更改整数或布尔值的所有字符串,我可以使它工作。 We also know that string on its own would work, eg df['A'][df['B']=='yes'] gives a boolean list. 我们也知道字符串本身就可以工作,例如df['A'][df['B']=='yes']给出一个布尔列表。

So any ideas how/why this is not working when combining these datatypes in this conditional expression? 那么在这个条件表达式中组合这些数据类型时,任何想法如何/为什么不起作用? What are the more pythonic ways to do what appears to be quite long-winded? 什么是pythonic方式做什么看起来很啰嗦?

Thanks 谢谢

In case this solution doesn't work for anyone, another situation that happened to me was that even though I was reading all data in as dtype=str (and therefore doing any string comparison should be OK [ie df[col] == "some string" ]), I had a column of all nulls, which becomes type float , which will give an error when comparing to a string. 如果这个解决方案对任何人都不起作用,那么发生在我身上的另一种情况是即使我正在以dtype=str读取所有数据(因此进行任何字符串比较都应该没问题[即df[col] == "some string" ]),我有一个所有空值的列,它变成了float类型,在与字符串比较时会产生错误。

To get around that, you can use .astype(str) to ensure a string to string comparison will be performed. 为了解决这个问题,您可以使用.astype(str)来确保执行字符串到字符串的比较。

I think you need add parentheses () to conditions, also better is use ix for selecting column with boolean mask which can be assigned to variable mask : 我认为你需要在条件中添加括号() ,更好的是使用ix选择具有布尔掩码的列,该布尔掩码可以分配给变量mask

mask = (df['A'].notnull()) & (df['B'].isnull()) & (df['C']=='yes')
print (mask)
0     True
1     True
2    False
3    False
dtype: bool

df.ix[mask, 'D'] = df.ix[mask, 'A']

print (df)
          A   B      C         D
0 -0.681771 NaN    yes -0.681771
1 -0.871787 NaN    yes -0.871787
2 -0.805301 NaN     no          
3  1.264103 NaN  maybe   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM