Python Practice › Pandas Programs › Program 77
Extract hashtags
Write a pandas program to extract hashtag words from tweets.
Output
Tweets tags 0 Pune #love [#love] 1 #boycottmovie [#boycottmovie] 2 enjoying #peace #calm [#peace, #calm]
Approach
str.findall(r"#\w+").
Solution
import pandas as pddf = pd.DataFrame({"Tweets": ["Pune #love", "#boycottmovie", "enjoying #peace #calm"]})df["tags"] = df["Tweets"].str.findall(r"#\w+")print(df)Output
Tweets tags 0 Pune #love [#love] 1 #boycottmovie [#boycottmovie] 2 enjoying #peace #calm [#peace, #calm]
Learn the concept
New to this topic? Read the lesson, then come back.