Condition "If" does not enter the structure

Asked

Viewed 174 times

-3

I’m having a problem with my code,I get an xlsx file, and I do the whole procedure correctly. but the file comes with some commas that need to be removed, and when I do the if to check if it has the comma it does not recognize and does not perform my conditional structure, and if there is no comma I do not want you to do anything, someone can help me where I am missing.

def main(bi):
    // Essa tabela possui virgula, mas ele não entra nessa condição abaixo
    if bi['Localizador [Outros, Aéreo, Cruise]'] == ',':
        bi['A'] = bi['Localizador [Outros, Aéreo,Cruise]'].str.split(',').str[0]
        bi['A'].fillna("null", inplace=True)

        def f(bi):
            if bi['A'] == "null":
                val =  bi['Localizador [Outros, Aéreo, Cruise]']
            else:
                    val = bi['A']
            return val
        bi['B'] = bi.apply(f, axis=1)
        bi['Localizador [Outros, Aéreo, Cruise]'] = bi['B']

        del bi['A']
        del bi['B']
    else:
        bi['Localizador [Outros, Aéreo, Cruise]']
  • Could exemplify a part of that xlsx file ?

  • is a common xml file, which I turn into a string and then into a DF. This column comes some numbers(Ex: 123456,123456,123456) when I select the split without the if it rotates normally and the column goes like this (Ex : '123456'), but when I do the if checking if it has comma in the column, it does not enter the split even containing comma

  • You would then have to check if the string has a comma, and the way it is, vc is checking if the string(column content) is only a string

  • I think you meant something like that ( if "," in bi['Finder [Others, Aerial, Cruise]']: but it didn’t work either

  • 2

    You can provide a sample of your data, and adapt the question so that it has an example minimum, complete and verifiable of the problem?

3 answers

0


I managed to find a way for him to check before if there is this comma in my DF is a bit of Gambiarra but it worked like this, I will publish the code used.

\\\ Crirei uma nova variavel para checar se existe "," na minha coluna com o (str.contains) ele retonar a quantidade de linhas que existem com virgulas
bi_check = bi[bi['Localizador [Outros, Aéreo, Cruise]'].str.contains(",") == True]

\\\ Aqui ele verifica quantas linhas com virgula possui no arquivo.
tam = len(bi_check)

\\\ se houver virgula o arquivo sera Maior que 0 então ele vai entrar no meu If e fazer o split
if tam > 0:
     bi['A'] = bi['Localizador [Outros, Aéreo, Cruise]'].str.split(',').str[0]
     bi['A'].fillna("null", inplace=True)
     def f(bi):
        if bi['A'] == "null":
            val =  bi['Localizador [Outros, Aéreo, Cruise]']
        else:
            val = bi['A']
        return val
     bi['B'] = bi.apply(f, axis=1)
     bi['Localizador [Outros, Aéreo, Cruise]'] = bi['B']
     del bi['A']
     del bi['B']
\\\ caso contrario ele vai me retornar a tabela normal.
else:
 bi = bi

0

If the value received from bi['Localizador [Outros, Aéreo, Cruise]'] for a string, so it will not enter the if because you set for it the condition of if the string is equal to ",". The right thing to do is to put the condition this way:

if "," in bi['Localizador [Outros, Aéreo, Cruise]']:
    ...

The way you were doing, you could get probation only if the string was only one ",".

  • I understood what he meant, but it did not work he continues without entering the if, as if the comma did not exist. my file when I am making this condition not and String it is a Dataframe

  • Putz. That way I won’t be able to help then because I don’t work with pandas, sorry.

0

You are doing the wrong check. At 3° line you are saying:

if bi['Localizador [Outros, Aéreo, Cruise]'] == ',': 

It’s other words you’re checking that this variable is equal to a comma. The correct check is you check if the variable contains a comma:

if "," in bi['Localizador [Outros, Aéreo, Cruise]']:
  • That was the same example that Jean commented just above, thank you but it didn’t work that way.

Browser other questions tagged

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