Suppose i have the following dataframe in python :
Country | Value |
---|---|
Arizona | 10 |
Los Angeles | 20 |
Alaska | 6 |
Huawai | NA |
First i want to bucket the values based on a range for example every 5, 0-5,5-10…
I want to make a map plot with plotly that has a discerte legend (the Bucket) and the color to be as the Value. Moreover in case the value is na i want to be plotted but with a different color, lets say black.
I’ve managed to make the plot but without the above constrains. Finally i want your opinion regarding the Bucket column. Below is the code that i wrote, is there any better way to write it?
def Bucket(row):
if row[‘GDP’] >10:
return ‘>10’
if row[‘GDP’] >20 and row[‘GDP’] < 10:
return ‘10-20’
if row[‘GDP’] > 20 and row[‘GDP’] < 30:
return ‘20-30’
if row[‘GDP’]==“NA”:
return “NA”
Thank you!
Currently my code for the plot is:
fig = px.choropleth(df, geojson=counties, locations=‘county’,
color=‘GDP’,
color_discrete_map =[[0, ‘rgb(240,240,240)’],
[10, ‘rgb(13,136,198)’],
[20, ‘rgb(191,247,202)’],
[30, ‘rgb(4,145,32)’],],
category_orders={
‘category’ : [
‘>10’,
‘10-20’,
‘20-30’]},
color_continuous_midpoint=2,
range_color=[0,1],
scope=“usa”,
hover_name = ‘county_name’
)
fig.update_layout(margin={“r”:0,“t”:0,“l”:0,“b”:0})
fig.show()