Python Practice › Pandas Programs › Program 77

Extract hashtags

EasyPandas Programs · 77 of 95

Write a pandas program to extract hashtag words from tweets.

Advertisement
Output
                  Tweets             tags
0             Pune #love          [#love]
1          #boycottmovie  [#boycottmovie]
2  enjoying #peace #calm  [#peace, #calm]

Approach

  1. str.findall(r"#\w+").

Solution

Python
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.

More pandas programs

← All 95 Pandas Programs

Leave a Comment