Reputation: 15
want to count the number of occurrence to be true which is satisfying below condition ,but it not showing the value in true or false but not able to count it,I don't know where i am doing wrong please have a look into my below code
from openpyxl import load_workbook
import openpyxl as xl
from openpyxl.utils.dataframe import dataframe_to_rows
import pandas as pd
import os
import xlwings as xw
import datetime
filelist_patch=[f for f in os.listdir() if f.endswith(".xlsx") and 'SEL' in f.upper() and '~' not in f.upper()]
print(filelist_patch[0])
wb = xl.load_workbook(filelist_patch[0],read_only=True,data_only=True)
wb_device=wb["de_exp"]
cols_device = [0,9,14,18,19,20,21,22,23,24,25]
#######################average count in vuln##############################
for row in wb_device.iter_rows(max_col=25):
cells = [cell.value for (idx, cell) in enumerate(row) if (
idx in cols_device and cell.value is not None)]
os_de=cells[1]
qca_de=cells[2]
file_data =((os_de=="cl") & (qca_de=='Q'))
print(sum(file_data))
getting a type error
TypeError Traceback (most recent call last)
<ipython-input-70-735a490062da> in <module>
30 file_data =((os_de=="client") & (qca_de=='Q'))(here i want to count the number of occurence that is in true
---> 31 print(sum(file_data))
32
33
TypeError: 'bool' object is not iterable
Upvotes: 0
Views: 1128
Reputation: 22738
Your question is very hard to read. Please use proper grammar and punctuation. I understand that you are probably not a native speaker, but that is no excuse to not form proper sentences that start with a capital letter and end with a period.
Please sort your own thoughts before you ask a question, and then write your thoughts in multiple short an concise sentenses.
Nonetheless, I'll try to guess what you are trying to say.
90% of your code is unrelated to the question, so I'll try to reform your question. If my guess is incorrect, of course my answer will be worthless, and I'd ask you to reword your question to be more precise.
Question: How to count the number of true statements in a number of conditions?
Details: Given a number of conditions (like os_de=="client"
and qca_de=='Q'
), how do I count the number of correct ones among them?
Attempt:
# Dummy data, to make this a short and reproducable example:
cells = ["", "cl", "D"]
os_de = cells[1]
qca_de = cells[2]
file_data = ((os_de=="cl") & (qca_de=='Q'))
print(sum(file_data))
Expected result value: 1
Actual result:
TypeError Traceback (most recent call last)
<ipython-input-70-735a490062da> in <module>
30 file_data = ((os_de=="client") & (qca_de=='Q'))
---> 31 print(sum(file_data))
32
33
TypeError: 'bool' object is not iterable
Both (os_de=="client")
and (qca_de=='Q')
are of type boolean
.
Using &
on them makes the result still be a boolean
. So if you try to use sum()
on it, it rightfully complains that the sum of a boolean does not make sense.
Sum can only be done over a collection of numbers.
You are almost there, though. Just instead of combining them with &
, make them a list instead.
# Dummy data, to make this a short and reproducable example:
cells = ["", "cl", "D"]
os_de = cells[1]
qca_de = cells[2]
file_data = [(os_de=="cl"), (qca_de=='Q')]
print(sum(file_data))
Which prints 1
, as expected: https://ideone.com/96Rghq
Try to include an ideone.com link in your questions in the future, this forces you to make your example code complete, simple and reproducable.
Upvotes: 1