使用Startswith +或Python 3 Pandas Select Dataframe [英] Python 3 Pandas Select Dataframe using Startswith + or

查看:156
本文介绍了使用Startswith +或Python 3 Pandas Select Dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找正确的语法来执行str.startswith,但是我想要多个条件.

Looking for the correct syntax to do a str.startswith but I want more than one condition.

我拥有的工作代码仅返回以字母"N"开头的办公室:

The working code I have only returns offices that start with the letter "N":

new_df = df[df['Office'].str.startswith("N", na=False)]

寻求一个返回可以以字母"N","M","V"或"R"开头的办公室的代码.以下似乎无效:

Seeking a code that returns offices that can start with the letters "N","M","V",or "R". The following doesn't seem to work:

    new_df = df[df['Office'].str.startswith("N|M|V|R", na=False)]

我想念什么?谢谢!

推荐答案

尝试一下:

df[df['Office'].str.contains("^(?:N|M|V|R)")]

或:

df[df['Office'].str.contains("^[NMVR]+")]

演示:

In [91]: df
Out[91]:
        Office
0        No-No
1         AAAA
2    MicroHard
3       Valley
4        vvvvv
5   zzzzzzzzzz
6  Risk is fun

In [92]: df[df['Office'].str.contains("^(?:N|M|V|R)")]
Out[92]:
        Office
0        No-No
2    MicroHard
3       Valley
6  Risk is fun

In [93]: df[df['Office'].str.contains("^[NMVR]+")]
Out[93]:
        Office
0        No-No
2    MicroHard
3       Valley
6  Risk is fun

这篇关于使用Startswith +或Python 3 Pandas Select Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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