[Python] tratamento de string em pandas

Discussão em 'Python' iniciado por Stack, Dezembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    Boa Noite Senhores, Tenho o seguinte Dataframe, importado de um xlsx.

    NUM_LEGISLACAO DSC_URL COD_SITUACAO ... DSC_TIPO num ano
    264 89.272/1984 NaN 2.0 ... NORMATIVO 89.272 1984
    265 90.396/1984 NaN 11.0 ... NORMATIVO 90.396 1984
    266 90.804/1985 NaN 2.0 ... CONCRETO 90.804 1985
    267 81195/1978 NaN NaN ... NaN 81195 1978
    268 5475/1905 NaN NaN ... NaN 05475 1905
    269 90.396/1984 NaN 11.0 ... NORMATIVO 90.396-A 1984


    Importei com o código abaixo:

    df1 = pd.read_excel(file01, sheet_name='Exportar Planilha')
    df1['num'], df1['ano'] = df1['NUM_LEGISLACAO'].str.split('/').str

    #notok
    #substitui '.' e '-' no campo 'num'
    df1['num'] = df1.num.str.replace('[\.-]', '')

    #formata com 5 digitos
    df1.num = df1.num.astype(str).str.rjust(5, '0')
    df1['num'] = df1.num.astype(str).str.zfill(5)


    Pretendo retirar '.' do campo 'num', e produzir o seguinte resultado:

    NUM_LEGISLACAO DSC_URL COD_SITUACAO ... DSC_TIPO num ano
    264 89.272/1984 NaN 2.0 ... NORMATIVO 89272 1984
    265 90.396/1984 NaN 11.0 ... NORMATIVO 90396 1984
    269 90.396/1984 NaN 11.0 ... NORMATIVO 90396A 1984


    Entretanto meu código não funciona e apresenta o seguinte erro:


    TypeError: Type aliases cannot be used with isinstance().
    ​

    Continue reading...

Compartilhe esta Página