Python 3.x 数据帧删除具有特定字符的字符串

Python 3.x 数据帧删除具有特定字符的字符串,python-3.x,pandas,string,dataframe,Python 3.x,Pandas,String,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({

我有一个包含大量文本数据的熊猫数据框。我要删除所有以*标记开头的行。因此,我尝试了以下一个小例子

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)))
它完全可以胜任这项工作。但是,当我将相同的函数应用于原始数据帧时,它不起作用。你能帮我找出这个问题吗

df2['NewsText'] = df2['NewsText'].map(lambda a: (re.sub(r'(?m)^\*.*\n?', '', a, flags=re.MULTILINE)))
df2.head()
给出你的示例数据 .str.split'\n'创建每个节的列表 .applylambda x:“\n”。join[y代表x中的y,如果“*”不在y中]使用列表理解删除带有*的每个句子,然后将其重新连接到字符串中。 您可以使用“”加入。加入或。加入 .applylambda x:[y表示x中的y,如果“*”不在y中]如果希望使用列表而不是长字符串。 || a| |--:|:-------------| |0 |*这需要消失| ||但这条线应该保持不变| ||*移除| ||*这也是| ||结束| |1 |*这需要消失| ||但这条线应该保持不变| ||*移除| ||*这也是| ||结束| 删除带有“*”的节 df['a']=df['a'].str.split'\n'.applylambda x:'\n'.join[y代表x中的y,如果'*'不在y中] 最终的 || a| |--:|:-------------| |0 |但这条线应该保持不变| ||结束| |1 |但这条线应该保持不变| ||结束|