Reputation: 225
I need to remove the HTML tags from all in a pandas column and just keep the description.
I have already removed the tags for one column but need to apply them to others - this is what I had and did;
Input:
df.description[1]
Output:
'<p>Das Ziel der <a href="http://swa-muc.de">Software Architektur München Gruppe</a> ist es, Menschen, die sich mit Software-Architektur auseinandersetzen wollen, in und um München regelmäßig zusammenzubringen, und einen persönlichen Erfahrungsaustausch zu etablieren. Dazu dienen neben der Meetup-Gruppe insbesondere der persönliche Erfahrungsaustausch, organisiert in informellen Treffen, Diskussion von Erfahrungsberichten und Case Studies bis hin zu Vorträge und Workshops. Diese werden auch über Twitter <a href="https://twitter.com/swamuc">@swamuc</a> und Meetup bekannt gegeben.</p>'
With I did;
read = df.description[1]
read_result = re.sub('<[^<]+?>', '', read)
print(read_result)
Output:
Das Ziel der Software Architektur München Gruppe ist es, Menschen, die sich mit Software-Architektur auseinandersetzen wollen, in und um München regelmäßig zusammenzubringen, und einen persönlichen Erfahrungsaustausch zu etablieren. Dazu dienen neben der Meetup-Gruppe insbesondere der persönliche Erfahrungsaustausch, organisiert in informellen Treffen, Diskussion von Erfahrungsberichten und Case Studies bis hin zu Vorträge und Workshops. Diese werden auch über Twitter @swamuc und Meetup bekannt gegeben.
This is all and well but I need to do this to the whole df.description
Pandas column.
This is what I have tried:
new_read = df.description[0:10148]
new_read_result = re.sub('<[^<]+?>', '', new_read)
print(new_read_result)
However, this is not functional and returns;
TypeError: expected string or bytes-like object
This is another method I'm trying for application:
df.description.apply(re.sub('<[^<]+?>', '', new_read))
But isn't working either.
How can I apply it to the whole pandas column?
Upvotes: 4
Views: 2288
Reputation: 4992
do the following
def parser(text):
return re.sub('<[^<]+?>', '', str(text))
result=df.description.apply(parser)
METHOD:2
df.description.apply(lambda x : re.sub('<[^<]+?>', '', str(text)))
Upvotes: 1
Reputation: 82765
Using lambda
in apply
:
Ex:
import pandas as pd
import re
df = pd.DataFrame({"description": ['<p>Hello</p>', '<p>World</p>']})
print( df.description.apply(lambda x: re.sub('<[^<]+?>', '', x)))
Output:
0 Hello
1 World
Name: description, dtype: object
Upvotes: 5