【问题标题】:Select rows from dataframe with a certain character after a '-'在“-”之后从具有特定字符的数据框中选择行
【发布时间】:2018-08-23 10:51:08
【问题描述】:

我有以下数据框:

import pandas as pd
test = pd.DataFrame({'A': 'A1-C-D-1 A22-C-D-22 A4-S-E-3'.split(),
             'B': [1, 2, 3]})

我想选择在第二个'-'之后有特定字符(例如'E')的行

非常欢迎任何想法!

【问题讨论】:

  • 感谢您的接受!如果您发现它们有用,您也可以对解决方案进行投票。谢谢。

标签: pandas dataframe querying


【解决方案1】:

选项 1
使用str.split + str.startswith 过滤:

test[test.A.str.split('-').str[2].str.startswith('E')]

          A  B
2  A4-S-E-3  3

选项 2
您可以在这里发挥创造力并使用str.extract + pd.Series.notna/notnull

test[test.A.str.extract('.*?-.*?-(E).*', expand=False).notna()]

          A  B
2  A4-S-E-3  3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 2022-11-01
    • 2022-01-08
    • 2023-01-18
    • 2015-03-29
    • 1970-01-01
    • 2019-07-02
    • 2013-09-26
    相关资源
    最近更新 更多