Search code examples
pythonpandasfunctionstripremoving-whitespace

Function to return stripped dataframe


I have a dataframe from CSV file:

import pandas as pd

filename = 'mike.csv'
main_df = pd.read_csv(filename)

I need a function that will strip all string columns' (there are also numeric columns) contents from whitespaces and then return such stripped dataframe. In the below function, the stripping seems to work fine, but I don't know how to return the stripped dataframe:

def strip_whitespace(dataframe):
    dataframe_strings = dataframe.select_dtypes(['object'])
    dataframe[dataframe_strings.columns] = dataframe_strings.apply(lambda x: x.str.strip())
    return # how to return a stripped dataframe here?

Full code:

import pandas as pd

filename = 'mike.csv'
main_df = pd.read_csv(filename)

def strip_whitespace(dataframe):
    dataframe_strings = dataframe.select_dtypes(['object'])
    dataframe[dataframe_strings.columns] = dataframe_strings.apply(lambda x: x.str.strip())
    return stripped_dataframe # ?

stripped_main_df = strip_whitespace(main_df) # should be stripped df

Solution

  • I believe need parameter skipinitialspace=True in read_csv:

    main_df = pd.read_csv(filename, skipinitialspace=True)
    

    And then stripping columns is not necessary.


    But if need use your function:

    return dataframe