¿Cómo extraer solo textos en hashtag usando tweepy?
Quiero extraer hashtags para mi proyecto de análisis de sentimientos, sin embargo, obtengo una lista de diccionario que contiene todos los hashtags junto con sus índices en el tweet. Solo quiero el texto.
Mi código :data = tweepy.Cursor(api.search, q, since=a[i], until=b[i]).items()
tweet_data = []
tweets = pd.DataFrame()
tweets['Tweet_ID'] = map(lambda tweet: tweet['id'], tweet_data)
tweets['Tweet'] = map(lambda tweet: tweet['text'].encode('utf-8'), tweet_data)
tweets['Date'] = map(lambda tweet: time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y')), tweet_data)
tweets['User'] = map(lambda tweet: tweet['user']['screen_name'], tweet_data)
tweets['Follower_count'] = map(lambda tweet: tweet['user']['followers_count'], tweet_data)
tweets['Hashtags']=map(lambda tweet: tweet['entities']['hashtags'], tweet_data)
Salida de corriente :df=pd.DataFrame({'Hashtags' : [{u'indices': [53, 65], u'text': u'Predictions'}, {u'indices': [67, 76], u'text': u'FreeTips'}, {u'indices': [78, 89], u'text': u'SoccerTips'}, {u'indices': [90, 103], u'text': u'FootballTips'}, {u'indices': [104, 110], u'text': u'Goals'}]})
Rendimiento esperado :df=pd.DataFrame({'Hashtags' :["u'Predictions'", "u'SoccerTips'", "u'FootballTips'", "u'Goals'"]})
Intenté usar varios métodos para aplanar / reducir / acceder a un diccionario anidado que contiene una lista de diccionarios. Por favor ayuda.
Error:Como sugirió @MSeifert, he probado su método. Se generó el siguiente error:
dt=tweet.entities.hashtags
pd.io.json.json_normalize(dt, 'hashtags')
pd.io.json.json_normalize(dt, 'hashtags')['text'].tolist()
Traceback (most recent call last): <\br>
File "<ipython-input-166-be11241611d6>", line 1, in <module>
dt=tweet.entities.hashtags
AttributeError: 'dict' object has no attribute 'entities'
También he intentado hacer esto: -
dx = tweets['Hashtags']
for key, value in dx.items():
print key, value
Con el siguiente error: -
Traceback (most recent call last):
File "<ipython-input-167-d66c278ec072>", line 2, in <module>
for key, value in dx.items():
File "C:\ANACONDA\lib\site-packages\pandas\core\generic.py", line 2740, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'items'
ACTUALIZACIÓNPuedo acceder a la parte de texto del diccionario de hashtags anidados
tweets['Hashtags'][1][1]['text']
Out[209]: u'INDvPAK'
Quiero crear un bucle para agregar todos los hashtags en la fila.