Reputation: 1043
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
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