2

I have the following pandas dataframe:

    Col
0   []
1   []
2   [(foo, bar), (foo, bar)]
3   []
4   []
5   []
6   []
7   [(foo, bar), (foo, bar)]

I would like to remove all the empty lists (*):

    Col
2   [(foo, bar), (foo, bar)]
7   [(foo, bar), (foo, bar)]

For the above I tried:

df = df.loc[df.Col != '[]']
df

and

df.pipe(lambda d: d[d['Col'] != '[]'])

However, none of them worked. So, my question is how can I remove all the empty lists from the dataframe like (*)?.

2 Answers 2

3

Slicing through the data frame as though the values were strings instead of lists may work:

df[df.astype(str)['Col'] != '[]']
Sign up to request clarification or add additional context in comments.

4 Comments

df.Col.str., I also tried that one before posting this question.
@tumbleweed You have to use astype to declare the series as a str in this case. .str implies you're planning to manipulate the variable after the assignment, or more simply, do something with it. It's pretty much there as an indicator for the preceding function to work with. e.g. str.replace() wouldn't work correctly if used without the column having already been made up of str values or having not been declared upon using astype.
@tumbleweed If this helps, from the doc itself - Series.astype: Cast object to input numpy.dtype Return a copy when copy = True
@tumbleweed So relating back to df.Col.str- dtype, being the str, hasn't been 'casted' :) Hope this helps. Have a good day!
1

You can check the length of the lists with .str accessor:

df[df.Col.str.len() != 0]

#                        Col
#3  [(foo, bar), (foo, bar)]
#6  [(foo, bar), (foo, bar)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.