【问题标题】:Partial queries in PandasPandas 中的部分查询
【发布时间】:2014-06-02 20:07:59
【问题描述】:

在 Pandas 中,可以执行以下操作:

df.query("some_column in ('foo', 'bar')")

它检索df 的子集,其中some_column 列的值为'foo''bar'

但是如果我想反其道而行之呢?例如,查找df 的子集,其中some_column 列中字符串的任何子字符串 匹配“foo”或“bar”。

如何在 Pandas 中做到这一点?

【问题讨论】:

    标签: python regex pandas


    【解决方案1】:

    使用string methods

    df['some column'].str.contains(r'foo|bar')
    

    例如:

    In [8]: Series(['foo', 'bar', 'fo', 'foo2']).str.contains(r'foo|bar')
    Out[8]: 
    0     True
    1     True
    2    False
    3     True
    dtype: bool
    

    【讨论】:

    • 谢谢!我怎样才能做到这一点,以便搜索不区分大小写?
    • 编译一个正则表达式模式并传递它。查看正则表达式文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多