检查熊猫列(系列)中是否存在元素

时间:2020-10-04 18:12:34

标签: python pandas numpy

这个问题已经以几种形式发布,但是由于某种原因,没有一种解决方案适用于我的用例。我有一个查找数字,它是我创建的类的一部分。我只想看一下该数字是否存在于我的数据框的一列中。以下是我尝试过的解决方案:

 # Where body_part.LookupNumber is a unique int and body_part.SymptomTable['BodyPartIllnessTypeId'] is a column of ints. 
 if body_part.LookupNumber in body_part.SymptomTable['BodyPartIllnessTypeId'].tolist():
      do something 
 
 # Get this error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() 

 # Fair enough, so then I tried this: 
 if body_part.LookupNumber in body_part.SymptomTable['BodyPartIllnessTypeId'].values:
     do something
 # Get this error ValueError: Lengths must match to compare

奇怪的是,当我运行它时,它会打印出来。在这种情况下,我已将我实例化为类对象:

if i.LookupNumber in i.SymptomTable['BodyPartIllnessTypeId'].values:
 print('worked')

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

非常感谢@Yonlif。就我而言,这是一个严重的错误。 `oscar.apps.dashboard.communication'` #singular 的类型实际上不是整数。