imhans4305
imhans4305

Reputation: 697

Removing Null Square Brackets from Pandas Dataframe

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

Upvotes: 3

Views: 417

Answers (4)

G.G
G.G

Reputation: 765

data.loc[data.C.apply(len)!=0]

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

Upvotes: 1

Binh
Binh

Reputation: 1173

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]

Upvotes: 1

mozway
mozway

Reputation: 260745

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]

Upvotes: 3

wwnde
wwnde

Reputation: 26676

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]

Upvotes: 1

Related Questions