【问题标题】:Deleteing rows in a pandas dataframe if it contains a certain string如果它包含特定字符串,则删除熊猫数据框中的行
【发布时间】:2022-11-27 03:07:47
【问题描述】:

我在数据框中有一个列列表,其中包含一个哈希标记后跟一个字符串或两个哈希标记后跟一个字符串。我想消除只包含一个井号的行。

df[df["column name"].str.contains("#") == False]

我试过使用上面的代码,但它删除了整个列。我希望它只会删除仅包含一个井号的行。我不知道该怎么办。

【问题讨论】:

  • 由于所有列至少包含一个“#”,df[“列名”].str.contains(“#”) 将始终为 True,因此 df[“列名”].str.contains(“#”) = = False 永远是 False;因此所有列都被跳过。 df[df["column name"].str.contains("##")] 行不通吗?

标签: python pandas data-filtering


【解决方案1】:

你能试试这个吗:

df['len']=df['column name'].str.count('#') #how many x expressions are there

df=df[df["len"]>1]

#one line

df=df[df['column name'].str.count('#')>1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2021-11-28
    • 2018-10-01
    • 2017-11-16
    • 2017-02-07
    相关资源
    最近更新 更多