Javier
Javier

Reputation: 831

Apply strip to column of lists in DataFrame

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

Answers (2)

ayorgo
ayorgo

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

Tim
Tim

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

Related Questions