Why is my choropleth not showing anything?
Question:
I want to show a map of the police deaths by states in the US.
Here is my code :
import pandas as pd #manipulation des donnees
import plotly.express as px
file="../data/PoliceDeaths.csv"
data = pd.read_csv(file, index_col=None)
df = pd.read_csv('deathbystate.csv', index_col=None)
state_data = pd.DataFrame()
state_data = data.state.value_counts().rename_axis('state_code').reset_index(name='number_of_deaths')
print(state_data)
fig = px.choropleth(state_data,
locations='state_code',
locationmode="USA-states",
color='number_of_deaths',
color_continuous_scale="Viridis_r",
scope="usa")
fig.show()
My "state_data" dataFrame looks like this when printed :
state_code number_of_deaths
0 TX 1880
1 CA 1639
2 NY 1574
3 US 1225
4 IL 1073
5 PA 901
6 OH 834
7 FL 820
8 KY 772
9 GA 698
10 MO 666
11 TN 607
12 MI 574
13 NC 549
14 AL 537
15 VA 523
16 OK 496
17 NJ 485
18 LA 470
19 IN 434
20 PR 363
21 SC 362
22 MA 360
23 MD 325
24 CO 305
25 WA 290
26 AR 284
27 RR 280
28 WI 275
29 AZ 273
30 MS 261
31 KS 257
32 MN 244
33 WV 208
34 IA 197
35 OR 182
36 NM 151
37 CT 144
38 DC 128
39 UT 128
40 MT 128
41 NV 120
42 NE 110
43 ME 86
44 ID 64
45 WY 57
46 HI 56
47 ND 55
48 SD 53
49 TR 51
50 RI 50
51 AK 48
52 NH 46
53 DE 41
54 VT 26
55 VI 14
56 GU 12
57 CZ 3
58 AS 3
59 MP 3
I looked how to do this and I can’t find the reason why my map is empty.
I though it would be the state_code that would be wrong but they are apparently right.
Do you know why is my map empty when I run my code ?
Looks like this : https://imgur.com/a/s8DOneA
Answers:
@Sadeuh I tried to plot the US map with state_data and it is working
fine. You may want to check "state_code" column if it has empty
characters etc. – Kaymal May 21 at 21:41
It was an empty character present before every state_code.
I want to show a map of the police deaths by states in the US.
Here is my code :
import pandas as pd #manipulation des donnees
import plotly.express as px
file="../data/PoliceDeaths.csv"
data = pd.read_csv(file, index_col=None)
df = pd.read_csv('deathbystate.csv', index_col=None)
state_data = pd.DataFrame()
state_data = data.state.value_counts().rename_axis('state_code').reset_index(name='number_of_deaths')
print(state_data)
fig = px.choropleth(state_data,
locations='state_code',
locationmode="USA-states",
color='number_of_deaths',
color_continuous_scale="Viridis_r",
scope="usa")
fig.show()
My "state_data" dataFrame looks like this when printed :
state_code number_of_deaths
0 TX 1880
1 CA 1639
2 NY 1574
3 US 1225
4 IL 1073
5 PA 901
6 OH 834
7 FL 820
8 KY 772
9 GA 698
10 MO 666
11 TN 607
12 MI 574
13 NC 549
14 AL 537
15 VA 523
16 OK 496
17 NJ 485
18 LA 470
19 IN 434
20 PR 363
21 SC 362
22 MA 360
23 MD 325
24 CO 305
25 WA 290
26 AR 284
27 RR 280
28 WI 275
29 AZ 273
30 MS 261
31 KS 257
32 MN 244
33 WV 208
34 IA 197
35 OR 182
36 NM 151
37 CT 144
38 DC 128
39 UT 128
40 MT 128
41 NV 120
42 NE 110
43 ME 86
44 ID 64
45 WY 57
46 HI 56
47 ND 55
48 SD 53
49 TR 51
50 RI 50
51 AK 48
52 NH 46
53 DE 41
54 VT 26
55 VI 14
56 GU 12
57 CZ 3
58 AS 3
59 MP 3
I looked how to do this and I can’t find the reason why my map is empty.
I though it would be the state_code that would be wrong but they are apparently right.
Do you know why is my map empty when I run my code ?
Looks like this : https://imgur.com/a/s8DOneA
@Sadeuh I tried to plot the US map with state_data and it is working
fine. You may want to check "state_code" column if it has empty
characters etc. – Kaymal May 21 at 21:41
It was an empty character present before every state_code.