How can I count csv file rows with pandas using &
and or
condition?
In the below code I want to count all rows that have True/False=FALSE and status = OK, and have '+' value in any of those columns openingSoon, underConstruction, comingSoon.
I've tried:
checkOne = df['id'].loc[(df['True/False'] == 'FALSE') & (df['status'] == 'OK') & (df['comingSoon'] == '+') or (df['openingSoon'] == '+') or (df['underConstruction'] == '+')].count()
error:
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().
Use |
for bitwise or
and for count True
s values filtering not necessary, use sum
:
Also for testing boolean df['True/False'] == False
is possible simplify by ~df['True/False']
checkOne = (~df['True/False'] &
(df['status'] == 'OK') &
(df['comingSoon'] == '+') |
(df['openingSoon'] == '+') |
(df['underConstruction'] == '+')).sum()
If True/False
are strings TRUE/FALSE
use:
checkOne = ((df['True/False'] == 'FALSE') &
(df['status'] == 'OK') &
(df['comingSoon'] == '+') |
(df['openingSoon'] == '+') |
(df['underConstruction'] == '+')).sum()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.