【问题标题】:Pandas DataFrame remove strings having a certain charactersPandas DataFrame 删除具有特定字符的字符串
【发布时间】:2020-05-25 19:36:26
【问题描述】:

我有一个带有大量文本数据的 pandas DataFrame。我想删除所有以“*”标记开头的行。因此,我尝试了一个小例子,如下所示。

string1 = '''* This needs to be gone
But this line should stay
*remove 
* this too
End'''

string2 = '''* This needs to be gone
But this line should stay
*remove 
* this too
End'''

df = pd.DataFrame({'a':[string1,string2]})
df['a'] = df['a'].map(lambda a: (re.sub(r'(?m)^\*.*\n?', '', a, flags=re.MULTILINE)))

它可以完美地完成这项工作。但是,当我将相同的功能应用于原始 DataFrame 时,它​​不起作用。你能帮我找出问题吗?

df2['NewsText'] = df2['NewsText'].map(lambda a: (re.sub(r'(?m)^\*.*\n?', '', a, flags=re.MULTILINE)))
df2.head()

Pease see the attached image of my original DataFrame

【问题讨论】:

    标签: python-3.x pandas string dataframe


    【解决方案1】:

    鉴于您的示例数据

    • .str.split('\n') 创建每个部分的列表
    • .apply(lambda x: '\n'.join([y for y in x if '*' not in y])) 使用列表推导来删除带有 * 的每个句子,然后将其连接回字符串。
      • 您可以通过' '.join''.join 加入
    • .apply(lambda x: [y for y in x if '*' not in y]) 如果你想要一个列表而不是一个长字符串。
    |    | a                         |
    |---:|:--------------------------|
    |  0 | * This needs to be gone   |
    |    | But this line should stay |
    |    | *remove                   |
    |    | * this too                |
    |    | End                       |
    |  1 | * This needs to be gone   |
    |    | But this line should stay |
    |    | *remove                   |
    |    | * this too                |
    |    | End                       |
    
    # remove sections with '*'
    df['a'] = df['a'].str.split('\n').apply(lambda x: '\n'.join([y for y in x if '*' not in y]))
    
    # final
    |    | a                         |
    |---:|:--------------------------|
    |  0 | But this line should stay |
    |    | End                       |
    |  1 | But this line should stay |
    |    | End                       |
    

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 2023-03-07
      • 2011-08-21
      • 2018-03-16
      相关资源
      最近更新 更多