1

I have a column in my dataframe that contains a list of names with such a structure:

df['name']=[

['anna','karen','',]
['', 'peter','mark','john']

]

I want to get rid of the empty strings i tried it with list comprehension

[[name for name in df['name'] if name.strip()] for df['name'] in df]

But that doesnt work at all, is there a way to do it? I also used the replace method from pandas by replacing the empty strings to nan but that also doesnt work as it always throws a key error...

df['name'].replace('', np.nan)
2
  • First things first, why do you have lists in a column? Commented Jun 12, 2020 at 11:18
  • the data above is dummy data, the data i have has to be stored in lists so i can do analysis on it... Commented Jun 12, 2020 at 11:28

2 Answers 2

3

You can do this using the filter function

df['name'] = [list(filter(None, sublist)) for sublist in df['name']]
Sign up to request clarification or add additional context in comments.

2 Comments

filter(None, sublist) is shorter.
Cool, I wasn't aware of that :)
2

The list comprehension isn't quite right, for df['name'] in df doesn't make sense. Try with:

df['name'] = [[s for s in l if s] for l in df['name']]

df = pd.DataFrame({'name':[['anna','karen','',], ['', 'peter','mark','john']]})

df['name'] = [[s for s in l if s] for l in df['name']]
print(df)
                 name
0        [anna, karen]
1  [peter, mark, john]

4 Comments

feel kinda dumb, thanks anyway! Btw do you know why the replace method from pandas throws keyerrors?
Because its ment for strings, not lists @voltage
I know thats why i unstacked them to series elements weird
Try with a column of strings and yo'll see @volt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.