kitchenprinzessin
kitchenprinzessin

Reputation: 1043

Python Dataframe : How to strip all values in a list in a column

I want to strip white spaces from all values in lists in the column 'Terms' in my dataframe:

df['Terms'] = df['Terms'].map(lambda x : x.strip())

This throws an error as the df['Terms'] type is list. Any help is appreciated.

AttributeError: 'list' object has no attribute 'values'

ANSWER: I created a function and then apply it to the column of the dataframe:

def strip_element(my_list):
    return [x.strip() for x in my_list]

df['Terms']=df['Terms'].apply(strip_element)

Upvotes: 0

Views: 1634

Answers (1)

U13-Forward
U13-Forward

Reputation: 71580

Agree with Rakesh, df['Terms'] = df['Terms'].str.strip() is the best solution, but since he already gave the solution, ypu can change map to apply:

df['Terms'] = df['Terms'].apply(lambda x: x.strip())

Upvotes: 2

Related Questions