0

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
5
  • This question was very hard to read. please provide more details about what you are trying to do and how we can replicate it. Your error looks like it is coming from code you don't even have in your post. And you want to count the number of occurrences in true? True is a boolean and you can't iterate over a boolean. Do you mean you want to count how many occurrences are true where (os_de=="client") & (qca_de=='Q'). You also don't have your code for this posted now Commented Apr 12, 2021 at 10:08
  • apologies ,yes i want to count how many occurrences are true where (os_de=="client") & (qca_de=='Q'). Commented Apr 12, 2021 at 10:11
  • updated the code in the edits Commented Apr 12, 2021 at 10:15
  • I dont get why use python if this kind of check can be done with simple function in excel itself, are there any cases which the excel + VBA doesn't cover ? Commented Apr 12, 2021 at 10:22
  • anything on this still struggling to count the value Commented Apr 12, 2021 at 11:28

1 Answer 1

1

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.


Reworded Question

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

Answer

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.