Home > front end >  Remove empty strings from a list of strings on each row in a pandas dataframe
Remove empty strings from a list of strings on each row in a pandas dataframe

Time:12-04

I have a pandas dataframe and one of the columns contains a list of strings e.g:

['', 'Hello', 'The house is warm', '', 'What time is it']

The strings are different for each row of the dataframe but all lists on each row contain empty strings. How can I remove these?

The column is called 'Description'.

I have tried the following methods:

df['Description'] = df['Description', [i for i in df['Description'] if i]]

while("" in df['Description']):
    df['Description'].remove("")
df['Description'] = [list(filter(None, sublist)) for sublist in df['Description']]

But none work. Thank you in advance!

CodePudding user response:

create new list and append only string that is not empty

use eval() if they are string representation of list

df['Description'] = df['Description'].apply(lambda x: [item for item in eval(x) if item != ''])
  • Related