Strip / trim all strings of a dataframe

后端 未结 6 950
夕颜
夕颜 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:17

    You can try:

    df[0] = df[0].str.strip()
    

    or more specifically for all string columns

    non_numeric_columns = list(set(df.columns)-set(df._get_numeric_data().columns))
    df[non_numeric_columns] = df[non_numeric_columns].apply(lambda x : str(x).strip())
    

提交回复
热议问题