i have a dataframe like this
Phrase Sentiment
[ good , movie ] positive
[wooow ,is , it ,very, good ] positive
[] negative
[] pOSTIVE
the column Phrase type is object and need to delete the rows containing [] and i don't know ho do it using python
like this:
Phrase Sentiment
[ good , movie ] positive
[wooow ,is , it ,very, good ] positive
You can check the presence of empty lists by str.len()==0
and filter the DF
based on this by performing a negation operation.
df[df.Phrase.str.len() != 0]
To know the rows where empty lists are present:
df.Phrase.str.len() == 0
0 False
1 False
2 True
3 True
Name: Phrase, dtype: bool
Incase there are empty strings present, their length would also equate to zero. In this case, filtering on the basis of their type would be helpful by using a custom function on map
.
df[df.Phrase.map(lambda x: len(x) if isinstance(x, list) else None) != 0]
If they are string representation of lists, then you could filter directly on them to get the subsetted DF
:
df[df.Phrase != "[]"]
empty lists []
evaluate to False
df[df.Phrase.astype(bool)]
Phrase Sentiment
0 [good, movie] positive
1 [woow, is, it, very, good] positive
setup
df = pd.DataFrame([
[['good', 'movie'], 'positive'],
[['woow', 'is', 'it', 'very', 'good'], 'positive'],
[[], 'negative'],
[[], 'pOSITIVE']
], columns=['Phrase', 'Sentiment'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With