Removing Null Square Brackets from Pandas Dataframe

Question:

I have a pandas dataframe in which some column contain empty sqaure brackets like below

Code

data = pd.DataFrame(dict(A=[5,3,5,6], C=[['man','talk'],['bar'],[],['bat','cat','mat']]))

DataFrame

    A   C
0   5   [man, talk]
1   3   [bar]
2   5   []
3   6   [bat, cat, mat]

I need to remove the rows containing empty square bracket

Required Dataframe

    A   C
0   5   [man, talk]
1   3   [bar]
2   6   [bat, cat, mat]

I tried data = data[data["C"].str.contains("[]") == False] but this is giving me error error: unterminated character set at position 0 . How to remove all those rows from a dataframe.
Thanks in Advance

Asked By: imhans4305

||

Answers:

You can check the length of the lists with str.len and slice using a boolean array when it is greater than 0:

data[data['C'].str.len().gt(0)]

or, convert to boolean (which seems to be the fastest approach):

data[data['C'].astype(bool)]

output:

   A                C
0  5      [man, talk]
1  3            [bar]
3  6  [bat, cat, mat]
Answered By: mozway

Please .str[index] and then dropna()

data[data['C'].str[0].notna()]



 A                C
0  5      [man, talk]
1  3            [bar]
3  6  [bat, cat, mat]
Answered By: wwnde

you can simply do this:

data[data['C'].map(lambda d: len(d)) > 0]

   A                C
0  5      [man, talk]
1  3            [bar]
3  6  [bat, cat, mat]
Answered By: Binh
data.loc[data.C.apply(len)!=0]

  A                C
0  5      [man, talk]
1  3            [bar]
3  6  [bat, cat, mat]
Answered By: G.G
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.