Strip / trim all strings of a dataframe

后端 未结 6 951
夕颜
夕颜 2020-11-27 13:04

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         


        
6条回答
  •  误落风尘
    2020-11-27 13:15

    Money Shot

    Here's a compact version of using applymap with a straightforward lambda expression to call strip only when the value is of a string type:

    df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
    

    Full Example

    A more complete example:

    import pandas as pd
    
    
    def trim_all_columns(df):
        """
        Trim whitespace from ends of each value across all series in dataframe
        """
        trim_strings = lambda x: x.strip() if isinstance(x, str) else x
        return df.applymap(trim_strings)
    
    
    # simple example of trimming whitespace from data elements
    df = pd.DataFrame([['  a  ', 10], ['  c  ', 5]])
    df = trim_all_columns(df)
    print(df)
    
    
    >>>
       0   1
    0  a  10
    1  c   5
    

    Working Example

    Here's a working example hosted by trinket: https://trinket.io/python3/e6ab7fb4ab

提交回复
热议问题