Compare a specific string in a field in python

1

The code proposition is to try to find the string 'WCDMA FDD Band I' in the device_2 ['Band'] column. If so, it should mark 'YES' in the device_2 ['WCDMA FDD Band I'] field and otherwise, it should mark 'NAO' in the device_2 ['WCDMA FDD Band I'] field.

The problem is that it can confirm whether or not it has found the string, however, it is marking all lines in the device_2 ['WCDMA FDD Band I'] field to 'NAO'.

Follow the code below:

# 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 is resolved 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 it was passed the column (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, regardless of the check. So I passed the "row" parameter so that the value would be set on each line after each check ... and worked fine:)

    
asked by anonymous 13.06.2018 / 16:05

0 answers