2

I have a dataframe which contians nested lists. Some of those lists are empty, or contain only whitespaces. e.g:

df=pd.DataFrame({'example':[[['  ', ' '],['Peter'],['   ', ' '],['bla','blaaa']]]})

for my further operations they are not allowed to be empty and cannot be deleted. Is there a way to fill them with e.g. 'some_string

i thought of something similar to df.example = [[[a.replace(' ','some_string')if all a in i =='\s'for a in i]for i in x] for x in df.example], but this yields an invalid syntax error, further it wouldnt just fill the list, but each whitespace in the list. Since i am still learning python, my idea of a solution might be too complicated or completely wrong.

i.e. the solution should look like:

     example
0   [[some_string], [Peter], [some_string], [bla, blaaa]

1 Answer 1

2

Using apply

Ex:

df=pd.DataFrame({'example':[[['  ', ' '],['Peter'],['   ', ' '],['bla','blaaa']]]})
df["example"] = df["example"].apply(lambda x: [i if "".join(i).strip() else ['some_string'] for i in x])
print(df)

Output:

                                             example
0  [[some_string], [Peter], [some_string], [bla, ...
  • Note: This will be slow if you data is very large because of the iteration.
Sign up to request clarification or add additional context in comments.

1 Comment

Or df.example = [[i if ''.join(i).strip() else ['some_string'] for i in x] for x in df.example]

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.