首页 > 解决方案 > 替换 HTML 标签不会改变数据框

问题描述

我正在尝试从我的数据框对象列中删除 html 标签,但它不起作用。

df.site.replace(to_replace=['<a href="', '</a>'],value='',inplace=True )

Out df.site:
0    <a href="http://twitter.com/download/iphone" r...
1    <a href="http://twitter.com/download/iphone" r...
2    <a href="http://twitter.com/download/iphone" r...
3    <a href="http://twitter.com/download/iphone" r...
4    <a href="http://twitter.com/download/iphone" r...

当我运行它时,没有任何变化。为什么?

标签: python-3.xpandas

解决方案


df = pd.DataFrame({"site":['<a href="http://example.com"</a> this is not ']})

df.site = df.site.str.replace(pat=r'(<a href=")|("</a>)',repl = "")

df.site
http://example.com this is not 

推荐阅读