'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
Solution 1:[1]
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]
Solution 2:[2]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Binh |
