【问题标题】:pandas: remove rows with 'tmp' in them [duplicate]熊猫:删除其中包含“tmp”的行[重复]
【发布时间】:2020-08-04 09:11:04
【问题描述】:

我在 Python 中有一个 pandas 数据框,数据框如下所示:

    id   count   table_size  table_name
     1    835        0.52    some_table
     2    543        1.02    another_table
     3    727        0.85    tmp_test_table
     4    834        1.46    empty_table
     5    552        0.99    tmp_my_table

我希望运行一个命令来完全删除以“tmp”开头的table_name 的所有行,因此对于我的示例,结果将是:

    id   count   table_size  table_name
     1    835        0.52    some_table
     2    543        1.02    another_table
     5    552        0.99    tmp_my_table

我不在乎 id 被弄乱了。

【问题讨论】:

  • table_name 中没有一个包含“temp”。你是说“tmp”吗?
  • 您的结果表中还包含一行带有“tmp”的行。

标签: python pandas dataframe row


【解决方案1】:

使用 str.startswith 过滤掉

df[~df.table_name.str.startswith('tmp')]

   id  count  table_size     table_name
0   1    835        0.52     some_table
1   2    543        1.02  another_table
3   4    834        1.46    empty_table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2019-06-19
    • 2016-09-09
    • 2016-01-30
    • 2021-09-05
    • 1970-01-01
    相关资源
    最近更新 更多