When loading data from a csv file, data is displayed correctly, but when i using the same data, but from the database, there is a problem with displaying data on a world map. Analysis of other data and graphing is correct. I tried changing the data type of coordinates, but it didn’t affect. Maybe I’m missing something explicit, but i still an inexperienced user.
This is how I get data from the csv:
terrorism = pd.read_csv('F:/data/terrorism-analysis/apps/data/globalterrorism.csv',
encoding='latin-1', low_memory=False,
usecols=['iyear', 'imonth', 'iday', 'country_txt', 'city', 'longitude', 'latitude',
'nkill', 'nwound', 'summary', 'target1', 'gname'])
And this is how I get data from the database:
connection = pymysql.connect(host='localhost',
user='root',
password='*****',
db='terrorism_bd',
charset='utf8mb4')
try:
sql = "SELECT iyear, imonth, iday, country_txt, city, longitude, latitude, nkill, nwound, summary, target1, gname FROM maintable "
terrorism = pd.read_sql(sql, connection)
finally:
connection.close()
In the case of using data from the database when choosing a country to display on the world map (in my case, terrorist acts and their coordinates), the following error is displayed:
127.0.0.1 - - [23/Jan/2020 02:16:53] "POST /_dash-update-component HTTP/1.1" 200 -
127.0.0.1 - - [23/Jan/2020 02:16:59] "POST /_dash-update-component HTTP/1.1" 200 -
[2020-01-23 02:16:59,103] ERROR in app: Exception on /_dash-update-component [POST]
Traceback (most recent call last):
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
...
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\dash\dash.py", line 1404, in dispatch
response.set_data(self.callback_map[output]["callback"](*args))
File "C:\Users\Andrey\AppData\Local\Programs\Python\Python37-32\lib\site-packages\dash\dash.py", line 1284, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
File "f:\data\terrorism-master\apps\world.py", line 144, in countries_on_map
for c in countries],
File "f:\data\terrorism-master\apps\world.py", line 144, in <listcomp>
for c in countries],
File "f:\data\terrorism-master\apps\world.py", line 132, in <listcomp>
'data': [go.Scattergeo(lon=[x + random.gauss(0.04, 0.03) for x in df[df['country_txt'] == c]['longitude']],
TypeError: can only concatenate str (not "float") to str
Another piece of code referenced by the error:
@app.callback(Output('map_world', 'figure'),
[Input('countries', 'value'), Input('years', 'value')])
def countries_on_map(countries, years):
df = terrorism[terrorism['country_txt'].isin(countries) & terrorism['iyear'].between(years[0], years[1])]
return {
'data': [go.Scattergeo(lon=[x + random.gauss(0.04, 0.03) for x in df[df['country_txt'] == c]['longitude']],
lat=[x + random.gauss(0.04, 0.03) for x in df[df['country_txt'] == c]['latitude']],
name=c,
hoverinfo='text',
marker={'size': 9, 'opacity': 0.65, 'line': {'width': .2, 'color': '#cccccc'}},
hovertext=df[df['country_txt'] == c]['city'].astype(str) + ', ' + df[df['country_txt'] == c]['country_txt'].astype(str)+ '<br>' +
[dt.datetime.strftime(d, '%d %b, %Y') for d in df[df['country_txt'] == c]['date']] + '<br>' +
'Perpetrator: ' + df[df['country_txt'] == c]['gname'].astype(str) + '<br>' +
'Target: ' + df[df['country_txt'] == c]['target1'].astype(str) + '<br>' +
'Deaths: ' + df[df['country_txt'] == c]['nkill'].astype(str) + '<br>' +
'Injured: ' + df[df['country_txt'] == c]['nwound'].astype(str) + '<br><br>' +
['<br>'.join(textwrap.wrap(x, 40)) if not isinstance(x, float) else '' for x in df[df['country_txt'] == c]['summary']])
for c in countries],
'layout': go.Layout(title='Terrorist Attacks ' + ', '.join(countries) + ' ' + ' - '.join([str(y) for y in years]),
font={'family': 'Palatino'},
titlefont={'size': 22},
paper_bgcolor='#eeeeee',
plot_bgcolor='#eeeeee',
width=1420,
height=650,
annotations=[{'text': 'Data: START Consortium', 'x': .2, 'y': -.13, 'showarrow': False}],
geo={'showland': True, 'landcolor': '#eeeeee',
'countrycolor': '#cccccc',
'showsubunits': True,
'subunitcolor': '#cccccc',
'subunitwidth': 5,
'showcountries': True,
'oceancolor': '#eeeeee',
'showocean': True,
'showcoastlines': True,
'showframe': False,
'coastlinecolor': '#cccccc',
'lonaxis': {'range': [df['longitude'].min()-1, df['longitude'].max()+1]},
'lataxis': {'range': [df['latitude'].min()-1, df['latitude'].max()+1]}
})
}
Please help me figure out what the error is and how to solve it.