Pandas: Extract hash attached word from twitter text from the specified column of a given DataFrame
25. Extract Hashtags from Twitter Text
Write a Pandas program to extract hash attached word from twitter text from the specified column of a given DataFrame.
Sample Solution:
Python Code :
import pandas as pd
import re as re
pd.set_option('display.max_columns', 10)
df = pd.DataFrame({
'tweets': ['#Obama says goodbye','Retweets for #cash','A political endorsement in #Indonesia', '1 dog = many #retweets', 'Just a simple #egg']
})
print("Original DataFrame:")
print(df)
def find_hash(text):
hword=re.findall(r'(?<=#)\w+',text)
return " ".join(hword)
df['hash_word']=df['tweets'].apply(lambda x: find_hash(x))
print("\Extracting#@word from dataframe columns:")
print(df)
Sample Output:
Original DataFrame:
tweets
0 #Obama says goodbye
1 Retweets for #cash
2 A political endorsement in #Indonesia
3 1 dog = many #retweets
4 Just a simple #egg
\Extracting#@word from dataframe columns:
tweets hash_word
0 #Obama says goodbye Obama
1 Retweets for #cash cash
2 A political endorsement in #Indonesia Indonesia
3 1 dog = many #retweets retweets
4 Just a simple #egg egg
For more Practice: Solve these Related Problems:
- Write a Pandas program to extract hashtag words from a tweet column using regex and then output a list of hashtags.
- Write a Pandas program to capture all words starting with '#' in a DataFrame column and then count the frequency of each hashtag.
- Write a Pandas program to extract hashtags from a text column and then create a new column listing the hashtags as a comma-separated string.
- Write a Pandas program to filter a DataFrame column to retrieve only hashtag words and then remove any duplicates.
Go to:
PREV : Extract Email from Column.
NEXT : Extract Mentions (@) from Tweets.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
