Folium Choropleth Highlight map elements is not working - python

This is my first question so hopefully, I'm asking it in a way that makes sense. If not, please correct me.
I want to use the highlight parameter in folium.Choropleth to achieve this sort of behaviour on mouse hover:
but it's not working.
I noticed one strange thing:
I also have folium.features.GeoJsonTooltip in my code and if I disable it, highlighting works. But when it's enabled, highlighting does not work. When folium.features.GeoJsonTooltip is enabled, the code compiles without errors but it's not highlighting countries as it should. All other functionalities work as expected.
folium.Choropleth(
geo_data=df1,
name="choropleth",
data=df3,
columns=["Country", "Estimate_UN"],
key_on="feature.properties.name",
fill_color="YlGnBu",
fill_opacity=0.8,
line_opacity=0.5,
legend_name="GDP Per Capita (in EUR)",
bins=bins,
highlight=True
).add_to(my_map)
Here's my full code:
import folium
import pandas
import geopandas
pandas.set_option('display.max_columns',25)
pandas.set_option('display.width',2000)
pandas.set_option('display.max_rows',300)
url = 'http://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita'
tables1 = pandas.read_html(url, match='Country/Territory')
df1 = tables1[0] # changes data from List to DataFrame
# makes it single index
df1.columns = ['Country', 'Region', 'Estimate_IMF', 'Year1', 'Estimate_UN', 'Year2', 'Estimate_WB', 'Year3']
# makes it two columns only (Country, Estimate_UN)
df1 = df1.drop(columns=['Region', 'Year1', 'Year2', 'Year3', 'Estimate_IMF', 'Estimate_WB'])
df1['Country'] = df1['Country'].map(lambda x: x.rstrip('*'))
df1['Country'] = df1['Country'].map(lambda x: x.strip())
df1['Country'] = df1['Country'].str.replace('United States', 'United States of America')
df1['Country'] = df1['Country'].str.replace('DR Congo', 'Dem. Rep. Congo')
df1['Country'] = df1['Country'].str.replace('Central African Republic', 'Central African Rep.')
df1['Country'] = df1['Country'].str.replace('South Sudan', 'S. Sudan')
df1['Country'] = df1['Country'].str.replace('Czech Republic', 'Czechia')
df1['Country'] = df1['Country'].str.replace('Bosnia and Herzegovina', 'Bosnia and Herz.')
df1['Country'] = df1['Country'].str.replace('Ivory Coast', """Côte d'Ivoire""")
df1['Country'] = df1['Country'].str.replace('Dominican Republic', 'Dominican Rep.')
df1['Country'] = df1['Country'].str.replace('Eswatini', 'eSwatini')
df1['Country'] = df1['Country'].str.replace('Equatorial Guinea', 'Eq. Guinea')
df1.drop(df1[df1['Estimate_UN'] == '—'].index, inplace = True)
df1['Estimate_UN'] = df1['Estimate_UN'].apply(lambda g:int(str(g)))
### --- Change 'GDP Per Capita' values in GeoJsonToolTip from format of 12345.0 (USD) to €11,604 --- ###
df2 = df1.copy()
df2['Estimate_UN'] = df2['Estimate_UN'].apply(lambda g:g*0.94) # Convert USD to EUR
df3 = df2.copy()
df2['Estimate_UN'] = df2['Estimate_UN'].apply(lambda g:str(int(g)))
df2['Estimate_UN'] = '€' + df2['Estimate_UN'].astype(str)
length = (df2['Estimate_UN'].str.len())
df2.loc[length == 7, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{3})(\d+)", r"\1,\2", regex=True)
df2.loc[length == 6, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{2})(\d+)", r"\1,\2", regex=True)
df2.loc[length == 5, 'Estimate_UN'] = df2[['Estimate_UN']].astype(str).replace(r"(\d{1})(\d+)", r"\1,\2", regex=True)
### --- Create map --- ###
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
df1 = world.merge(df1, how='left', left_on=['name'], right_on=['Country'])
df1 = df1.dropna(subset=['Estimate_UN'])
df2 = world.merge(df2, how='left', left_on=['name'], right_on=['Country'])
df2 = df2.dropna(subset=['Estimate_UN'])
df3 = world.merge(df3, how='left', left_on=['name'], right_on=['Country'])
df3 = df3.dropna(subset=['Estimate_UN'])
my_map = folium.Map(location=(39.22753573470106, -3.650093262568073),
zoom_start=2,
tiles = 'https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
attr = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye,Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
min_zoom=2,
min_lot=-179,
max_lot=179,
min_lat=-65,
max_lat=179,
max_bounds=True)
### --- Add tooltip --- ###
gdp = folium.FeatureGroup(name="GDP")
gdp.add_child(folium.GeoJson(data=df2, tooltip = folium.features.GeoJsonTooltip(
fields=['Country','Estimate_UN'],
aliases=['Country:','GDP Per Capita:'],
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"),
localize = True),
style_function= lambda y:{
'stroke':'false',
'opacity':'0',
}))
### --- Color countries --- ###
bins = [100,1000,5000,10000,20000,35000,50000,112000]
folium.Choropleth(
geo_data=df1,
name="choropleth",
data=df3,
columns=["Country", "Estimate_UN"],
key_on="feature.properties.name",
fill_color="YlGnBu",
fill_opacity=0.8,
line_opacity=0.5,
legend_name="GDP Per Capita (in EUR)",
bins=bins,
highlight=True
).add_to(my_map)
my_map.add_child(gdp)
my_map.save('index.html')
I'm looking forward to your suggestions on why GeoJsonTooltip is stopping the highlight parameter from working!

My understanding is that folium.Choropleth() has a highlighting feature, but no popup or tooltip feature. If you want to use the tooltip and popup functions, use folium.Geojson(). I will respond with a df3 of the data you presented.
I have implemented my own color map for color coding. The index of
the color map is modified according to the number of colors. See this
for more information about our own colormaps.
The tooltip is introduced as you set it up. We have also added a
pop-up feature. You can add supplementary information. If you don't
need it, please delete it.
The color fill is specified by the style function, which gets the
color name from the estimated value for the previously specified
colormap function. At the same time, a highlight function is added to
change the transparency of the map drawing. The basic code can be found here.
import folium
from folium.features import GeoJsonPopup, GeoJsonTooltip
import branca
bins = [5000,25000,45000,65000,112000]
my_map = folium.Map(location=(39.22753573470106, -3.650093262568073),
zoom_start=2,
tiles = 'https://server.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
attr = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye,Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
min_zoom=2,
min_lot=-179,
max_lot=179,
min_lat=-65,
max_lat=179,
max_bounds=True)
colormap = branca.colormap.LinearColormap(
vmin=df3['Estimate_UN'].quantile(0.0),
vmax=df3['Estimate_UN'].quantile(1),
colors=["red", "orange", "lightblue", "green", "darkgreen"],
caption="Original Colormap",
index=bins
)
tooltip = folium.features.GeoJsonTooltip(
fields=['Country','Estimate_UN'],
aliases=['Country:','GDP Per Capita:'],
style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;"),
localize=True)
popup = GeoJsonPopup(
fields=['Country','Estimate_UN'],
aliases=['Country:','GDP Per Capita:'],
localize=True,
labels=True,
style="background-color: yellow;",
)
folium.GeoJson(data=df3,
tooltip=tooltip,
popup=popup,
style_function= lambda y:{
"fillColor": colormap(y["properties"]["Estimate_UN"]),
'stroke':'false',
'opacity': 0.4
},
highlight_function=lambda x: {'fillOpacity': 0.8},
).add_to(my_map)
colormap.add_to(my_map)
# my_map.save('index.html')
my_map

Related

Trying to create a streamlit app that uses user-provided URLs to scrape and return a downloadable df

I'm trying to use this create_df() function in Streamlit to gather a list of user-provided URLs called "recipes" and loop through each URL to return a df I've labeled "res" towards the end of the function. I've tried several approaches with the Streamlit syntax but I just cannot get this to work as I'm getting this error message:
recipe_scrapers._exceptions.WebsiteNotImplementedError: recipe-scrapers exception: Website (h) not supported.
Have a look at my entire repo here. The main.py script works just fine once you've installed all requirements locally, but when I try running the same script with Streamlit syntax in the streamlit.py script I get the above error. Once you run streamlit run streamlit.py in your terminal and have a look at the UI I've create it should be quite clear what I'm aiming at, which is providing the user with a csv of all ingredients in the recipe URLs they provided for a convenient grocery shopping list.
Any help would be greatly appreciated!
def create_df(recipes):
"""
Description:
Creates one df with all recipes and their ingredients
Arguments:
* recipes: list of recipe URLs provided by user
Comments:
Note that ingredients with qualitative amounts e.g., "scheutje melk", "snufje zout" have been ommitted from the ingredient list
"""
df_list = []
for recipe in recipes:
scraper = scrape_me(recipe)
recipe_details = replace_measurement_symbols(scraper.ingredients())
recipe_name = recipe.split("https://www.hellofresh.nl/recipes/", 1)[1]
recipe_name = recipe_name.rsplit('-', 1)[0]
print("Processing data for "+ recipe_name +" recipe.")
for ingredient in recipe_details:
try:
df_temp = pd.DataFrame(columns=['Ingredients', 'Measurement'])
df_temp[str(recipe_name)] = recipe_name
ing_1 = ingredient.split("2 * ", 1)[1]
ing_1 = ing_1.split(" ", 2)
item = ing_1[2]
measurement = ing_1[1]
quantity = float(ing_1[0]) * 2
df_temp.loc[len(df_temp)] = [item, measurement, quantity]
df_list.append(df_temp)
except (ValueError, IndexError) as e:
pass
df = pd.concat(df_list)
print("Renaming duplicate ingredients e.g., Kruimige aardappelen, Voorgekookte halve kriel met schil -> Aardappelen")
ingredient_dict = {
'Aardappelen': ('Dunne frieten', 'Half kruimige aardappelen', 'Voorgekookte halve kriel met schil',
'Kruimige aardappelen', 'Roodschillige aardappelen', 'Opperdoezer Ronde aardappelen'),
'Ui': ('Rode ui'),
'Kipfilet': ('Kipfilet met tuinkruiden en knoflook'),
'Kipworst': ('Gekruide kipworst'),
'Kipgehakt': ('Gemengd gekruid gehakt', 'Kipgehakt met Mexicaanse kruiden', 'Half-om-halfgehakt met Italiaanse kruiden',
'Kipgehakt met tuinkruiden'),
'Kipshoarma': ('Kalkoenshoarma')
}
reverse_label_ing = {x:k for k,v in ingredient_dict.items() for x in v}
df["Ingredients"].replace(reverse_label_ing, inplace=True)
print("Assigning ingredient categories")
category_dict = {
'brood': ('Biologisch wit rozenbroodje', 'Bladerdeeg', 'Briochebroodje', 'Wit platbrood'),
'granen': ('Basmatirijst', 'Bulgur', 'Casarecce', 'Cashewstukjes',
'Gesneden snijbonen', 'Jasmijnrijst', 'Linzen', 'Maïs in blik',
'Parelcouscous', 'Penne', 'Rigatoni', 'Rode kidneybonen',
'Spaghetti', 'Witte tortilla'),
'groenten': ('Aardappelen', 'Aubergine', 'Bosui', 'Broccoli',
'Champignons', 'Citroen', 'Gele wortel', 'Gesneden rodekool',
'Groene paprika', 'Groentemix van paprika, prei, gele wortel en courgette',
'IJsbergsla', 'Kumato tomaat', 'Limoen', 'Little gem',
'Paprika', 'Portobello', 'Prei', 'Pruimtomaat',
'Radicchio en ijsbergsla', 'Rode cherrytomaten', 'Rode paprika', 'Rode peper',
'Rode puntpaprika', 'Rode ui', 'Rucola', 'Rucola en veldsla', 'Rucolamelange',
'Semi-gedroogde tomatenmix', 'Sjalot', 'Sperziebonen', 'Spinazie', 'Tomaat',
'Turkse groene peper', 'Veldsla', 'Vers basilicum', 'Verse bieslook',
'Verse bladpeterselie', 'Verse koriander', 'Verse krulpeterselie', 'Wortel', 'Zoete aardappel'),
'kruiden': ('Aïoli', 'Bloem', 'Bruine suiker', 'Cranberrychutney', 'Extra vierge olijfolie',
'Extra vierge olijfolie met truffelaroma', 'Fles olijfolie', 'Gedroogde laos',
'Gedroogde oregano', 'Gemalen kaneel', 'Gemalen komijnzaad', 'Gemalen korianderzaad',
'Gemalen kurkuma', 'Gerookt paprikapoeder', 'Groene currykruiden', 'Groentebouillon',
'Groentebouillonblokje', 'Honing', 'Italiaanse kruiden', 'Kippenbouillonblokje', 'Knoflookteen',
'Kokosmelk', 'Koreaanse kruidenmix', 'Mayonaise', 'Mexicaanse kruiden', 'Midden-Oosterse kruidenmix',
'Mosterd', 'Nootmuskaat', 'Olijfolie', 'Panko paneermeel', 'Paprikapoeder', 'Passata',
'Pikante uienchutney', 'Runderbouillonblokje', 'Sambal', 'Sesamzaad', 'Siciliaanse kruidenmix',
'Sojasaus', 'Suiker', 'Sumak', 'Surinaamse kruiden', 'Tomatenblokjes', 'Tomatenblokjes met ui',
'Truffeltapenade', 'Ui', 'Verse gember', 'Visbouillon', 'Witte balsamicoazijn', 'Wittewijnazijn',
'Zonnebloemolie', 'Zwarte balsamicoazijn'),
'vlees': ('Gekruide runderburger', 'Half-om-half gehaktballetjes met Spaanse kruiden', 'Kipfilethaasjes', 'Kipfiletstukjes',
'Kipgehaktballetjes met Italiaanse kruiden', 'Kippendijreepjes', 'Kipshoarma', 'Kipworst', 'Spekblokjes',
'Vegetarische döner kebab', 'Vegetarische kaasschnitzel', 'Vegetarische schnitzel'),
'zuivel': ('Ei', 'Geraspte belegen kaas', 'Geraspte cheddar', 'Geraspte grana padano', 'Geraspte oude kaas',
'Geraspte pecorino', 'Karnemelk', 'Kruidenroomkaas', 'Labne', 'Melk', 'Mozzarella',
'Parmigiano reggiano', 'Roomboter', 'Slagroom', 'Volle yoghurt')
}
reverse_label_cat = {x:k for k,v in category_dict.items() for x in v}
df["Category"] = df["Ingredients"].map(reverse_label_cat)
col = "Category"
first_col = df.pop(col)
df.insert(0, col, first_col)
df = df.sort_values(['Category', 'Ingredients'], ascending = [True, True])
print("Merging ingredients by row across all recipe columns using justify()")
gp_cols = ['Ingredients', 'Measurement']
oth_cols = df.columns.difference(gp_cols)
arr = np.vstack(df.groupby(gp_cols, sort=False, dropna=False).apply(lambda gp: justify(gp.to_numpy(), invalid_val=np.NaN, axis=0, side='up')))
# Reconstruct DataFrame
# Remove entirely NaN rows based on the non-grouping columns
res = (pd.DataFrame(arr, columns=df.columns)
.dropna(how='all', subset=oth_cols, axis=0))
res = res.fillna(0)
res['Total'] = res.drop(['Ingredients', 'Measurement'], axis=1).sum(axis=1)
res=res[res['Total'] !=0] #To drop rows that are being duplicated with 0 for some reason; will check later
print("Processing complete!")
return res
Your function create_df needs a list as an argument but st.text_input returs always a string.
In your streamlit.py, replace this df_download = create_df(recs) by this df_download = create_df([recs]). But if you need to handle multiple urls, you should use str.split like this :
def create_df(recipes):
recipes = recipes.split(",") # <--- add this line to make a list from the user-input
### rest of the code ###
if download:
df_download = create_df(recs)
# Output :

nested drop downs with xlwings

I'm trying to generate nested drop downs with xlwings, a python module enabling linking python scripts to VBA functions. I was able to do this using the xslxwriter module using excel's =indirect(cell) formula but I can't seem to find any equivalent in xlwings.
Well trying to refine my question I found the answer.
Here is how to do nested drop downs with xlsxwriter.
import xlsxwriter
# open workbook
workbook = xlsxwriter.Workbook('nested_drop_downs_with_xlsxwriter.xlsx')
# data
countries = ['Mexico', 'USA', 'Canada']
mexican_cities = ['Morelia', 'Cancun', 'Puebla']
usa_cities = ['Chicago', 'Florida', 'Boston']
canada_cities = ['Montreal', 'Toronto', 'Vancouver']
# add data to workbook
worksheet = workbook.add_worksheet("sheet1")
worksheet.write_column(0, 0, countries)
worksheet.write(0, 1, countries[0])
worksheet.write_column(1, 1, mexican_cities)
worksheet.write(0, 2, countries[1])
worksheet.write_column(1, 2, usa_cities)
worksheet.write(0, 3, countries[2])
worksheet.write_column(1, 3, canada_cities)
# name regions
workbook.define_name('Mexico', '=sheet1!$B2:$B4')
workbook.define_name('USA', '=sheet1!$C2:$C4')
workbook.define_name('Canada', '=sheet1!$D2:$D4')
#
worksheet.data_validation('A10', {'validate': 'list', 'source': '=sheet1!$A$1:$A$3'})
worksheet.data_validation('B10', {'validate': 'list', 'source': '=INDIRECT($A$10)'})
workbook.close()
And here is how to do nested drop downs with xlwings:
def main():
wb = xw.Book.caller()
sheet = wb.sheets('Sheet1')
# data
countries = ['Mexico', 'USA', 'Canada']
mexican_cities = ['Morelia', 'Cancun', 'Puebla']
usa_cities = ['Chicago', 'Florida', 'Boston']
canada_cities = ['Montreal', 'Toronto', 'Vancouver']
# add data to workbook
sheet.range('A1:A3').options(transpose=True).value = countries
sheet.range('B1').value = countries[0]
sheet.range('B2').options(transpose=True).value= mexican_cities
sheet.range('C1').value = countries[1]
sheet.range('C2').options(transpose=True).value = usa_cities
sheet.range('D1').value = countries[2]
sheet.range('D2').options(transpose=True).value = canada_cities
# name regions <-------- naming regions with a dollar sign was the fix!
sheet.range('$B$2:$B$4').api.name.set('Mexico')
sheet.range('$C$2:$C$4').api.name.set('USA')
sheet.range('$D$2:$D$4').api.name.set('Canada')
sheet.range('A10').api.validation.delete()
sheet.range('A10').api.validation.add_data_validation(type=3, formula1='=Sheet1!$A$1:$A$3')
sheet.range('B10').api.validation.delete()
sheet.range('B10').api.validation.add_data_validation(type=3, formula1='=INDIRECT($A$10)')
if __name__ == "__main__":
xw.Book("demo1.xlsm").set_mock_caller()
main()

Hover over an area a text should appear

I am currently viewing the neighborhoods. However, when I hover the mouse over it, I want a text to be displayed with the neighborhood and the average price. I have pasted my code and my card what I made. Below you can see how I would like it.
How can I display a text when I hover over it?
import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap
map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
map_ams_price.choropleth(
geo_data=r'C:\neighbourhoods.geojson',
data=df,
columns=['neighbourhood_cleansed', 'price'],
key_on='feature.properties.neighbourhood',
fill_color='BuPu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Avg',
reset=True,
tooltip=folium.features.GeoJsonTooltip(fields=['neighbourhood_cleansed', 'price'],
labels=True,
sticky=False),
)
map_ams_price
Minium Example
# the price is the avg price
d = {'neighbourhood_cleansed': ['Oostelijk Havengebied - Indische Buurt', 'Centrum-Oost',
'Centrum-West', 'Zuid', 'De Baarsjes - Oud-West', 'Bos en Lommer',
'De Pijp - Rivierenbuurt', 'Oud-Oost', 'Noord-West', 'Westerpark',
'Slotervaart', 'Oud-Noord', 'Watergraafsmeer',
'IJburg - Zeeburgereiland', 'Noord-Oost', 'Buitenveldert - Zuidas',
'Geuzenveld - Slotermeer', 'De Aker - Nieuw Sloten', 'Osdorp',
'Bijlmer-Centrum', 'Gaasperdam - Driemond', 'Bijlmer-Oost'],
'price': [20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0]}
df = pd.DataFrame(data=d)
You can find the geojson here https://pastecode.io/s/a76fdvey
As I mentioned in the comments, in order to give tooltips in the choropleth map, they have to be in the geojson file, not in the dataframe value, in order to be displayed. So I used geopandas for the geojson file to be used, combined the data frames and added the price information to the geojson file. The column names in the original data frame have been modified to match the geojson file. It can also be used as a label by adding an alias name. The tooltip can be styled, so I added that as well.
import json
import requests
import geopandas as gpd
url = "http://data.insideairbnb.com/the-netherlands/north-holland/amsterdam/2021-09-07/visualisations/neighbourhoods.geojson"
gpd_geo = gpd.read_file(url)
gpd_geo = gpd_geo.merge(df, on='neighbourhood')
geo_json_data = gpd_geo.to_json()
import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap
from folium.features import GeoJsonPopup, GeoJsonTooltip
map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
choropleth = folium.Choropleth(
geo_data=geo_json_data,
data=df,
columns=['neighbourhood', 'price'],
key_on='feature.properties.neighbourhood',
fill_color='BuPu',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Avg',
reset=True,
highlight=True,
).add_to(map_ams_price)
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(fields=['neighbourhood', 'price'],
aliases=['neighbourhood:', 'average_price:'],
labels=True,
localize=True,
sticky=False,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",)
)
map_ams_price

Plotly Choropleth Map Not Showing Up

I'm trying to display a Plotly Choropleth Map in Jupyter Notebooks (I'm a beginner with this type of stuff) and for some reason it won't display correctly.
The csv file I am using for it can be found here:
https://www.kaggle.com/ajaypalsinghlo/world-happiness-report-2021
Here is the code leading up to the choropleth:
# here we're assigning the hover data columns to use for our choropleth map below
hover_data_cols_df = ['Country', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
df.groupby('Year').Country.count()
and here is the code for the actual choropleth:
choropleth_map = px.choropleth(df,
locations="Country",
color='Life Ladder',
hover_name = 'Life Ladder',
hover_data = hover_data_cols_df,
color_continuous_scale = px.colors.sequential.Oranges,
animation_frame="Year"
).update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,);
iplot(choropleth_map)
I'm not getting any error messages attached to it currently, however when I check my console log on my browser, I do find this error:
Wolrd-Happiness-Report.ipynb:1 Uncaught ReferenceError: require is not defined
at <anonymous>:1:17
at t.attachWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at t.insertWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at x._insertOutput (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at x.onModelChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at Object.l [as emit] (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at e.emit (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at c._onListChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
I'm not too sure if this is related or not!
Thanks all!
Your task requires a setting that associates a country name with a country on the map. It requires that the location mode be the country name.
import pandas as pd
df = pd.read_csv('./data/world-happiness-report.csv', sep=',')
df.sort_values('year', ascending=True, inplace=True)
hover_data_cols_df = ['Country name', 'year', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
import plotly.express as px
fig = px.choropleth(df,
locations="Country name",
locationmode='country names',
color='Life Ladder',
hover_name = 'Life Ladder',
hover_data = hover_data_cols_df,
color_continuous_scale = px.colors.sequential.Oranges,
animation_frame="year"
)
fig.update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,);
fig.show()

how do i Determine a Cut-Off or Threshold When Working With Fuzzymatcher in python

Please help on the photo is a screenshot of my output and code as well, how do i use the best_match_score I NEED TO FILTER BY THE RETURNED "PRECISION SCORE" THE COLUMN ONLY COMES AFTER THE MERGE (i.e. JUST RETURN EVERYTHING with 'best_match_score' BELOW -1.06)
import fuzzymatcher
import pandas as pd
import os
# pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
REDCAP = pd.read_csv(r"C:\Users\Selamola\Desktop\PythonThings\FuzzyMatching\REDCAP Form A v1 and v2 23 Feb 211.csv")
covidSheet = pd.read_csv(r"C:\Users\Selamola\Desktop\PythonThings\FuzzyMatching\Cases missing REC ID 23 Feb 211.csv")
Data_merge = fuzzymatcher.fuzzy_left_join(covidSheet, REDCAP,
left_on=['Participant Name', 'Particfipant Surname', 'Screening Date',
'Screening Date', 'Hospital Number', 'Alternative Hospital Number'],
right_on=['Patient Name', 'Patient Surname', 'Date Of Admission',
'Date Of Sample Collection', 'Hospital Number', 'Hospital Number'])
# Merged_data = pd.merge(REDCAP, covidSheet, how='left',
# left_on=['Patient Name', 'Patient Surname'],
# right_on=['Participant Name', 'Particfipant Surname'])
# Data_merge.to_csv(r'C:\Users\Selamola\Desktop\PythonThings\FuzzyMatching\DataMacth.csv')
print(Data_merge)
This seems very straightforward unless I'm missing something. Be sure to try read the documentation about slicing data in pandas.
mask = Data_merge['best_match_score'] < .1.06
filtered_data = Data_merge[mask]

Categories