pandas :如何消除以特定字符结尾的值的行? [英] pandas: how to eliminate rows with value ending with a specific character?

查看:63
本文介绍了 pandas :如何消除以特定字符结尾的值的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫DataFrame,如下所示:

I have a pandas DataFrame as follows:

mail = DataFrame({'mail' : ['adv@gmail.com', 'fhngn@gmail.com', 'foinfo@yahoo.com', 'njfjrnfjrn@yahoo.com', 'nfjebfjen@hotmail.com', 'gnrgiprou@hotmail.com', 'jfei@hotmail.com']})

如下所示:

                    mail
0          adv@gmail.com
1        fhngn@gmail.com
2       foinfo@yahoo.com
3   njfjrnfjrn@yahoo.com
4  nfjebfjen@hotmail.com
5  gnrgiprou@hotmail.com
6       jfei@hotmail.com

我想做的是过滤掉(删除)所有列邮件中以 @ gmail.com结尾的行。

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'.

推荐答案

您可以使用 str.endswith 并使用

mail[~mail['mail'].str.endswith('@gmail.com')]

会产生以下内容:

                    mail
2       foinfo@yahoo.com
3   njfjrnfjrn@yahoo.com
4  nfjebfjen@hotmail.com
5  gnrgiprou@hotmail.com
6       jfei@hotmail.com

熊猫还有许多其他向量化的字符串操作,可通过 .str 访问器进行访问。其中许多都是Python自己的字符串方法立即为您所熟悉,但它们会以处理 NaN 值的方式构建。

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.

这篇关于 pandas :如何消除以特定字符结尾的值的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆