Parse Input and structure the output # Keywords from tweets

↘锁芯ラ 提交于 2019-12-24 07:57:44

问题


I am trying to put all the #keywords from the tweetText into a separate column along with other columns. I have not mentioned other columns as they would only create confusion.

The tweetText which does not have #keywords shall be deleted and those which have shall be fished out and put them in different column.

I am kind of lost in the part where I need to filter the #Keywords from the tweetText.

Input: TweetsID, Tweets (has many more columns)

714602054988275712,I'm at MK Appartaments in Dobele
714600471512670212,"Baana bicycle counter.Today: 9 Same time last week: 7 Trend: ↑28% This year: 60 811 Last year: 802 079 #Helsinki #pyöräily #cycling"
714598616703320065,"Just posted a photo @ Moscow, Russia"
714593900053180416,We're #hiring! Read about our latest #job opening here: CRM Specialist #lifeinspiringcareers #Moscow #Sales
714591942949138434,Just posted a photo @ Kfc 
714591380660731904,Homeless guide on my festival of tours from locals for locals #открытаякарта. Shot by Alexandr
714591338977579009,"Who we are? #edmonton #edm #edmlife #edms #edmlifestyle #edmfamily #edmgirls #edmlov"

Expected Output: tweetId, hashKey (will have other columns too)

714600471512670212,#Helsinki #pyöräily #cycling
714593900053180416,#hiring! #lifeinspiringcareers #Moscow #Sales
714591380660731904,#открытаякарта
714591338977579009,#edmonton #edm #edmlife #edms #edmlifestyle #edmfamily #edmgirls #edmlov"

Code:

import pandas as pd

df1 = pd.read_csv('Turkey_28.csv')

key_word = df1[['tweetID', 'tweetText']].set_index('tweetID')['tweetText']

key_word = key_word.dropna().apply(lambda x: eval(x))
key_word = key_word[key_word.apply(type) == dict]

 #I am lost in this section on how to select the hash keywords?   
def get_key_words(x):                                                       
    return pd.Series(x['tweetText'], 

key_word = key_word.apply(get_key_word)

df2 = pd.concat([coords, df1.set_index('tweetID').reindex(coords.index)], axis=1)

df2.to_csv('Turkey_key_word.csv', index=True)

Appreciate the suggestions.

Edit One:

When parsed input in the chosen answer I get some Syntax Errors

Code:

import re
import pandas as pd

df = pd.readcsv('Turkey_Text.csv')
tweet_column = ['tweetText']
for idx in range(len(tweet_column)):
    tweet = tweet_column[idx]
    hashtag_list = re.findall(r('#\w+)', tweet)
    tweet_column[idx] = " ".join(hashtag_list)

print tweet_column[idx]

Error:

File "keyword_split.py", line 9
    tweet_column[idx] = " ".join(hashtag_list)
               ^
SyntaxError: invalid syntax

Expected Output

714600471512670212,#Helsinki 
714600471512670212,#pyöräily 
714600471512670212,#cycling
714593900053180416,#hiring! 
714593900053180416,#lifeinspiringcareers 
714593900053180416,#Moscow 
714593900053180416,#Sales
714591380660731904,#открытаякарта
714591338977579009,#edmonton 
714591338977579009,#edm 
714591338977579009,#edmlife 
714591338977579009,#edms 
714591338977579009,#edmlifestyle 
714591338977579009,#edmfamily 
714591338977579009,#edmgirls 
714591338977579009,#edmlov"

回答1:


Use python and regular expressions. It will make your life a lot easier. The regular expression r'#(\w+)'would work well in this instance.

I don't fully understand the flow of your code, since I don't have much experience with searching CSVs with panda, but if you were to isolate the tweet and return a string of the keywords/hashtags to that column by my understanding of conventional python logic, it might look something like this...

import re

for idx in range(len(tweet_column)):
    tweet = tweet_column[idx]
    hashtag_list = re.findall(r('#\w+)', tweet)
    tweet_column[idx] = " ".join(hashtag_list)

Here's another example



来源:https://stackoverflow.com/questions/38044375/parse-input-and-structure-the-output-keywords-from-tweets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!