Apply strip to column of lists in DataFrame

Viewed 1064

I have a DataFrame df with a column named AD that contains lists like [' foo','fo oooo oo ',' fffo ','fofofofo'] (note random blankspaces at begining and end of each item in list). How can I apply strip function to each element in lists in the whole column?

2 Answers

Just rewrite the column with a list with strip applied to each element:

df['AD'] = [[val.strip() for val in sublist] for sublist in df['AD'].values]

You can do

df['AD'] = df['AD'].map(lambda l: list(map(lambda x: x.strip('o'), l)))

or if you only need to remove whitespaces

df['AD'] = df['AD'].map(lambda l: list(map(str.strip, l)))
Related