Extract the hash attached word from a tweet

In this python pandas program, we will extract the hash attached word from a tweet using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import re library.
  3. Create a dataframe using pd.DataFrame().
  4. Create a function to extract the hash attached word from the record.
  5. Extract the hash attached word from the record using re.findall(r'(?<=#)\w+’,text).
  6. It will extract the hash attached word from the record and the return will give word as the output of the function.
  7. Now apply this function to the Tweets column of the dataframe using df[‘Tweets’].apply(lambda x: find_hash(x)).
  8. The lambda function will apply the created function to each row to extract the hash attached word and store it in the new column.
  9. Print the output.
				
					import pandas as pd
import re
d = {'Tweets':['Pune #love','#boycottmovie','enjoying #peace']}
df = pd.DataFrame(d)
print(df)
def find_hash(text):
    hword=re.findall(r'(?<=#)\w+',text)
    return " ".join(hword)
df['extracted_word']=df['Tweets'].apply(lambda x: find_hash(x))
print("Extracting#@word from dataframe columns:")
print(df)
				
			

Output :

				
					0            Tweets
0       Pune #love
1    #boycottmovie
2  enjoying #peace
Extracting#@word from dataframe columns:
            Tweets extracted_word
0       Pune #love           love
1    #boycottmovie   boycottmovie
2  enjoying #peace          peace
				
			

extract email from a specified column of a given DataFrame

extract only words from a column of a DataFrame

Leave a Comment