Pandas trim leading & trailing white space in a dataframe

前端 未结 1 1689
生来不讨喜
生来不讨喜 2020-12-20 10:03

develop a function that Trims leading & trailing white space.

here is a simple sample, but real file contains far more complex rows and columns.

         


        
相关标签:
1条回答
  • 2020-12-20 11:04

    I think need check if values are strings, because mixed values in column - numeric with strings and for each string call strip:

    df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
    print (df)
                         A    B     C
    0                  A b    2   3.0
    1                  NaN    2   3.0
    2               random   43   4.0
    3  any txt is possible  2 1  22.0
    4                        23  99.0
    5                 help   23   NaN
    

    If columns have same dtypes, not get NaNs like in your sample for numeric values in column B:

    cols = df.select_dtypes(['object']).columns
    df[cols] = df[cols].apply(lambda x: x.str.strip())
    print (df)
                         A    B     C
    0                  A b  NaN   3.0
    1                  NaN  NaN   3.0
    2               random  NaN   4.0
    3  any txt is possible  2 1  22.0
    4                       NaN  99.0
    5                 help  NaN   NaN
    
    0 讨论(0)
提交回复
热议问题