Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (19.9k points)

One of the column in my df stores a list, and some of the raws have empty items in the list. For example:

[]

["X", "Y"]

[]

etc...

How can only take the raw whose list is not empty?

The following code does not work.

df[df["col"] != []] # ValueError: Lengths must match to compare

df[pd.notnull(df["col"])] # The code doesn't issue an error but the result includes an empty list

df[len(df["col"]) != 0] # KeyError: True

3 Answers

0 votes
by (25.1k points)

You can do it like this:

df[df["col"].str.len() != 0]

0 votes
by (1.9k points)

To extract all the data from a column containing an empty list, you can filter out rows in DataFrame by using lambda function.

import pandas as pd

sample_data={

‘col’:[ [ ], [ “X”, “Y”], [ ], [“Z] ]

}

df=pd.DataFrame(sample_data)

filtered_data= df[ df[‘col’ ].apply(lambda x: len(x)>0) ]

print(filtered_data)

This will filter out the empty list and output will be: 

    col

1 [X,Y]

3 [Z]

0 votes
by (1.6k points)

You are trying to filter rows in your DataFrame in the columns where the values are an empty list([]). To perform this operation you can use lambda function to retrieve all values in the specific column and need to check if the length of the list is greater than 0.

Code:

import pandas as pd

d = {“column_1”: [[], [‘x’, ‘y’], [], [‘A’], [2,3]]}

df = pd.DataFrame(d)

filtered_df = df[df["column_1"].apply(lambda x: len(x) > 0)]

print(filtered_df)

Output:

      Column1

1     [‘x’,’y’]

3     [‘A’]

4     [2,3]

1.4k questions

32.9k answers

507 comments

693 users

...