Reputation: 831
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?
Upvotes: 4
Views: 1790
Reputation: 3902
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)))
Upvotes: 0
Reputation: 2843
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]
Upvotes: 4