I have a pandas DataFrame as follows:
mail = DataFrame({'mail' : ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']})
that looks like:
mail 0 [email protected] 1 [email protected] 2 [email protected] 3 [email protected] 4 [email protected] 5 [email protected] 6 [email protected]
What I want to do is to filter out (elimiante) all those rows in which the value in the column mail ends with '@gmail.com'.
You can use str.endswith and negate the result of the boolean Series with ~:
str.endswith
~
mail[~mail['mail'].str.endswith('@gmail.com')]
Which produces:
mail 2 [email protected] 3 [email protected] 4 [email protected] 5 [email protected] 6 [email protected]
Pandas has many other vectorised string operations which are accessible through the .str accessor. Many of these are instantly familiar from Python's own string methods, but come will built in handling of NaN values.
.str
NaN
1.4m articles
1.4m replys
5 comments
57.0k users