Home > Enterprise >  Removing Null Square Brackets from Pandas Dataframe
Removing Null Square Brackets from Pandas Dataframe

Time:02-18

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

CodePudding user response:

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]

CodePudding user response:

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]

CodePudding user response:

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]
  • Related