1
The purpose of the code is to try to find the string 'WCDMA FDD Band I' in the column device_2['Band']. If so, it should dial 'YES' in the field device_2['WCDMA FDD Band I'] and otherwise it should dial 'NO' in the field device_2['WCDMA FDD Band I'].
The problem is that it can confirm whether or not it has found the string, but it is marking all lines of the field device_2['WCDMA FDD Band I'] as 'NAO'.
Follows code:
# importando bibliotecas
import pandas as pd
import openpyxl
from openpyxl import load_workbook
# Criando coluna de strings
Band = [['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800),WCDMA FDD Band I,WCDMA FDD Band II,WCDMA FDD Band V,WCDMA FDD Band VIII'],
['GSM 1800,GSM 1900,GSM 900'], ['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800),HSDPA,WCDMA FDD Band I,WCDMA FDD Band VIII'],
['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800)']]
# Criando data frame com a coluna acima
device_2 = pd.DataFrame(Band, columns=['Band'])
# Adicionando a coluna flag para marcar presença da string WCDMA FDD BAND I
device_2['WCDMA FDD Band I'] = ''
# Percorrendo as linhas do data frame para verificação da string
for index, row in device_2.iterrows():
if 'WCDMA FDD Band I' in row['Band']:
**device_2['WCDMA FDD Band I'] = 'SIM'**
print('Encontrou')
else:
**device_2['WCDMA FDD Band I'] = 'NAO'**
print('Nao encontrou')
The problem was solved as follows:
# importando bibliotecas
import pandas as pd
import openpyxl
from openpyxl import load_workbook
# Criando coluna de strings
Band = [['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800),WCDMA FDD Band I,WCDMA FDD Band II,WCDMA FDD Band V,WCDMA FDD Band VIII'],
['GSM 1800,GSM 1900,GSM 900'], ['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800),HSDPA,WCDMA FDD Band I,WCDMA FDD Band VIII'],
['GSM 1800,GSM 1900,GSM 900,GSM850 (GSM800)']]
# Criando data frame com a coluna acima
device_2 = pd.DataFrame(Band, columns=['Band'])
# Adicionando a coluna flag para marcar presença da string WCDMA FDD BAND I
device_2['WCDMA FDD Band I'] = ''
# Percorrendo as linhas do data frame para verificação da string
for index, row in device_2.iterrows():
if 'WCDMA FDD Band I' in row['Band']:
**row['WCDMA FDD Band I'] = 'SIM'**
print('Encontrou')
else:
**row['WCDMA FDD Band I'] = 'NAO'**
print('Nao encontrou')
When the column was passed (device_2['WCDMA FDD Band I']) to receive YES or NO, the code understood that it was to set a single value in the entire column, independent of the verification. Therefore, I passed the parameter "Row" so that the value was set in each line after each check... and it worked :)
What will be the type of
row
in that case?– Woss
In this case the type will be <class 'str'>
– João Lucas Flauzino
device_2['WCDMA FDD Band I']
will be the same for all lines? The way he did he is always assigning yes/no to the same object.– Woss
device_2['WCDMA FDD Band I'] would be a fixed column to set the presence of the string 'WCDMA FDD Band I' in device['Band'] placing the values YES or NO. In case I would have to create another object to receive the NO?
– João Lucas Flauzino
Then elaborate a [mcve], because the logic of the code you posted is correct (https://repl.it/@acwoss/Limpingoldlacevoxels), the problem must be somewhere else.
– Woss
@Andersoncarloswoss put a summary of the above code... as you had said, it finds the string, however, at the time of marking the field it assumes the condition of ELSE.
– João Lucas Flauzino
So, as I said before, you’re assigning the yes/no to the same variable every time. When you leave the
for
, it will only have the value of the last iteration - which in this example is "no".– Woss
@Andersoncarloswoss managed to solve the problem. I was not passing the "Row" parameter, without it the code was not understanding that it was to set the values per line and put the 'NAO' for the whole column. Thank you very much for your attention.
– João Lucas Flauzino
Then work out a response describing how you solved
– Woss