如何使用&
andor
条件计算带有熊猫的csv文件行?
在下面的代码中,我要计算所有具有True / False = FALSE和status = OK的行,并且在openSoon,underConstruction,comingSoon的任何列中都具有“ +”值。
我试过了:
checkOne = df['id'].loc[(df['True/False'] == 'FALSE') & (df['status'] == 'OK') & (df['comingSoon'] == '+') or (df['openingSoon'] == '+') or (df['underConstruction'] == '+')].count()
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.9/site-packages/pandas/core/generic.py", line 1329, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用|
按位or
和计True
价值观过滤不是必需的,使用sum
:
同样对于测试布尔值df['True/False'] == False
,可以通过简化~df['True/False']
checkOne = (~df['True/False'] &
(df['status'] == 'OK') &
(df['comingSoon'] == '+') |
(df['openingSoon'] == '+') |
(df['underConstruction'] == '+')).sum()
如果True/False
是字符串,请TRUE/FALSE
使用:
checkOne = ((df['True/False'] == 'FALSE') &
(df['status'] == 'OK') &
(df['comingSoon'] == '+') |
(df['openingSoon'] == '+') |
(df['underConstruction'] == '+')).sum()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句