In this python pandas program, we will extract the hash attached word from a tweet using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Import re library.
- Create a dataframe using pd.DataFrame().
- Create a function to extract the hash attached word from the record.
- Extract the hash attached word from the record using re.findall(r'(?<=#)\w+’,text).
- It will extract the hash attached word from the record and the return will give word as the output of the function.
- Now apply this function to the Tweets column of the dataframe using df[‘Tweets’].apply(lambda x: find_hash(x)).
- The lambda function will apply the created function to each row to extract the hash attached word and store it in the new column.
- 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