在pandas用Dataframe中的某个值进行if逻辑判断时,进行以下代码书写:
if max_data.user_id != 'ALL':
print(max_data['user_id'])
ValueError Traceback (most recent call last)
<ipython-input-28-d57a8b5ab65d> in <module>
----> 1 if max_data.user_id != 'ALL':
2 print(max_data['user_id'])
~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1476 raise ValueError("The truth value of a {0} is ambiguous. "
1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478 .format(self.__class__.__name__))
1479
1480 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
解决办法:
if (max_data.user_id != 'ALL').bool():
print(max_data['user_id'])