[Python] Strip / trim all strings of a dataframe

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    Cleaning the values of a multitype data frame in python/pandas, I want to trim the strings. I am currently doing it in two instructions :

    import pandas as pd

    df = pd.DataFrame([[' a ', 10], [' c ', 5]])

    df.replace('^\s+', '', regex=True, inplace=True) #front
    df.replace('\s+$', '', regex=True, inplace=True) #end

    df.values


    This is quite slow, what could I improve ?

    Continue reading...

Compartilhe esta Página