Compare a specific string in a python field

Asked

Viewed 132 times

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?

  • In this case the type will be <class 'str'>

  • 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.

  • 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?

  • 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.

  • @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.

  • 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".

  • @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.

  • Then work out a response describing how you solved

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.