删除包含用方括号包裹的特定字符串的行?

Dropping Rows that Contain a Specific String wrapped in square brackets?

我正在尝试删除包含包含在列中的字符串的行。我想删除所有包含字符串“[removed]”、“[deleted]”的值。 我的 df 看起来像这样:

  Comments

1 The main thing is the price appreciation of the token (this determines the gains or losses more 
  than anything). Followed by the ecosystem for the liquid staking asset, the more opportunities 
  and protocols that accept the asset as collateral, the better. Finally, the yield for staking 
  comes into play.

2 [deleted]

3 [removed]

4 I could be totally wrong, but sounds like destroying an asset and claiming a loss, which I 
  believe is fraudulent. Like someone else said, get a tax guy - for this year anyway and then 
  you'll know for sure. Peace of mind has value too.

我试过了df[df["Comments"].str.contains("removed")==False] 但是当我尝试保存数据框时,它仍然没有被删除。

编辑: 我的完整代码

import pandas as pd
sol2020 = pd.read_csv("Solana_2020_Comments_Time_Adjusted.csv")
sol2021 = pd.read_csv("Solana_2021_Comments_Time_Adjusted.csv")
df = pd.concat([sol2021, sol2020], ignore_index=True, sort=False)
df[df["Comments"].str.contains("deleted")==False]
df[df["Comments"].str.contains("removed")==False]

试试这个

我已经为评论栏创建了一个数据框并使用了我自己的评论,但它应该适合你

import pandas as pd

sample_data = { 'Comments': ['first comment whatever','[deleted]','[removed]','last comments whatever']}

df = pd.DataFrame(sample_data)

data = df[df["Comments"].str.contains("deleted|removed")==False]

print(data)

我得到的输出

 Comments
0  first comment whatever
3  last comments whatever

你可以这样做:

new_df = df[~(df['Comments'].str.startswith('[') & df['Comments'].str.endswith(']'))].reset_index(drop=True)

输出:

>>> new_df
                                            Comments
0  The main thing is the price appreciation of th...
3  I could be totally wrong, but sounds like dest...

这将删除该行的 Comments 列的值以 [ 开头并以 ] 结尾的所有行。