0

Hello I am trying to count the 'OK' instances in a specific cell in a word table. Here is my code:

from docx2python import docx2python
import pandas as pd

doc_result = docx2python('Results3.docx')

counterOK = 0
counterNOK = 0

df = pd.DataFrame(doc_result.body[0], columns= [0,1,2,3])
print(df)

req = df.loc[9][1]
print(req)

if (req == 'OK'):
    counterOK+=1
elif (req == 'NOK'):
    counterNOK+=1
else :
    (print('sad'))

print(counterOK)
print(counterNOK)

Why do I always end up in the else branch?

Here is the Word Table I used.

Nemo
  • 85
  • 2
  • 11

1 Answers1

0

Why do I always end up in the else branch?

Because req always looks at df.loc[9][0]. Instead iterate through the dataframe.

Eg.

for index, row in df.iterrows():
    if row[1] == 'OK':
        ...
        ...
Krish
  • 1,044
  • 9
  • 20
  • Yes I know, I intend to look in this specific location for now. I will create the loop later on by changing this line df = pd.DataFrame(doc_result.body[i], columns= [0,1,2,3]) to access several tables. Right now print(req) prints: ['OK'] however the if condition isn't fulfilled. – Nemo May 14 '20 at 11:01
  • 1
    @Nemo `['OK']` is not the same as `'OK'`. Look At @Krish answer as he indexes the row `row[1]`. – Joshua Nixon May 14 '20 at 11:10
  • 1
    Hmmm. Try `print(type(req))` and check whether it is a string class or list class. If `req` is an iterable just read out the first value of `req` – Krish May 14 '20 at 11:12
  • @Krish It's a list class. I followed your advice and changed the code to `if (req[0] == 'OK')` and it works now. Thanks a lot. – Nemo May 14 '20 at 11:34