0

I am currently working on a project, the idea is to extract tweets (with geo enabled) from a Hashtag and to print a map (with Folium). Inside the map, I should have markers according to the user locations and when I click at the marker I should have the text of the tweet. But currently I only have a map and the markers.

This is my code :

import pandas as pd
import folium, ast

locations = pd.read_csv('tweets.csv', usecols=[3]).dropna()

l_locations = []

for loc in locations.values:
    l_locations.append(ast.literal_eval(loc[0])['coordinates'])

print_tweet_map = folium.Map(location=[48.864716, 2.349014], zoom_start=8, tiles='Mapbox Bright')

for geo in l_locations:
    folium.Marker(location=geo).add_to(print_tweet_map)

print_tweet_map.save('index.html')

Can you guys help me to print the markers and the text details of the tweet ? Thanks in advance.

PS : I have currently : Europe map

E Ajanthan
  • 77
  • 1
  • 1
  • 9
  • What have you tried in order to take user input and/or display tweet details? – Aaron3468 Oct 04 '17 at 08:39
  • Hello @Aaron3468 I know how I can print the text details by changing folium.Marker(location=geo).add_to(print_tweet_map) to folium.Marker(location=geo, popup=tweets).add_to(print_tweet_map). But I don't know how to retrieve tweets with my actual code – E Ajanthan Oct 04 '17 at 08:43
  • What data exists inside `tweets.csv`? It looks like the third value in each row corresponds to location, but perhaps it would be helpful if you posted a short excerpt of the file. Twitter's api seems to provide json and presumably the first field would be a user_id. You'll want to extract the field containing the tweet's permalink and the field containing its text. I believe the `popup` argument is all that folium requires to make text show on clicks. The `tooltip` argument allows a preview on hover. – Aaron3468 Oct 04 '17 at 08:52
  • Only 4 columns : created_at, user, text, geo (i posted some lines from it below) – E Ajanthan Oct 04 '17 at 09:18

2 Answers2

0

Some lines of the csv file :

created_at,user,text,geo
2017-09-30 15:28:56,yanceustie,"RT @ChelseaFC: .@AlvaroMorata and @marcosalonso03 have been checking out the pitch ahead of kick-off..., null
2017-09-30 15:48:18,trendinaliaVN,#CHEMCI just started trending with 17632 tweets. More trends at ... #trndnl,"{'type': 'Point', 'coordinates': [21.0285, 105.8048]}"
E Ajanthan
  • 77
  • 1
  • 1
  • 9
0

Examine read_csv closely and then use it to retrieve the tweet text as well. Reading folium documentation, the popup seems the most relevant place to put each tweet's text.

Also you iterate over the same things 2 times. You can reduce those into 1 iteration loop that places a tweet on the map. (Imagine the map as your empty list you were appending to). No need to be overly sequential.

import pandas as pd
import folium, ast

frame = pd.read_csv('tweets.csv', usecols=[2, 3], names=['text', 'location'], header=None).dropna()

print_tweet_map = folium.Map(location=[48.864716, 2.349014], zoom_start=8, tiles='Mapbox Bright')

for index, item in frame.iterrows():
    loc = item.get('location')
    text = item.get('text')
    geo = ast.literal_eval(loc[0])['coordinates']

    folium.Marker(location=geo, popup=text) \ 
                .add_to(print_tweet_map)

print_tweet_map.save('index.html')

This should work, or very close to work, but I don't have a proper computer handy for testing.

Aaron3468
  • 1,734
  • 16
  • 29
  • I have this error : Traceback (most recent call last): File "print_map.py", line 11, in geo = ast.literal_eval(loc[0])['coordinates'] File "C:\Users\Ajanthan\AppData\Local\Programs\Python\Python36-32\lib\ast.py", line 85, in literal_eval return _convert(node_or_string) File "C:\Users\Ajanthan\AppData\Local\Programs\Python\Python36-32\lib\ast.py", line 84, in _convert raise ValueError('malformed node or string: ' + repr(node)) ValueError: malformed node or string: <_ast.Name object at 0x04F84CB0> – E Ajanthan Oct 04 '17 at 09:55
  • And also why did you use a \ in line 13 ? – E Ajanthan Oct 04 '17 at 09:56
  • @E Ajanthan Ah, seems like you will need to debug what `loc[0]` means. I probably messed up naming the columns when I used `read_csv`. The `\` just extends a line to the next line; it can make long lines a lot easier to read. – Aaron3468 Oct 04 '17 at 10:07
  • I have this error now : print_tweet_map = folium.Map(location=[48.864716, 2.349014], zoom_start=8, tiles='Mapbox Bright') for index, item in frame.iterrows(): loc = item.get('location') text = item.get('text') geo = ast.literal_eval(loc[0])['coordinates'] folium.Marker(location=geo, popup=text) \ .add_to(print_tweet_map) print_tweet_map.save('index.html') Can you help me with this please :) – E Ajanthan Oct 04 '17 at 10:55