本文介绍了不支持Python/Folium/clopeth:TypeError:ufunc&39;isnan;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在洛杉矶的邮政编码上绘制一张全息图,以便显示/突出显示数据帧的某一列的值。到目前为止,我的代码收到了以下错误消息: 输入类型不支持‘TypeError:ufunc’isnan‘,根据强制转换规则’‘Safe’‘,无法将输入安全地强制为任何受支持的类型 ‘
我花了几天时间研究Google,Stack,研究纪录片,查看YouTube教程,但我仍然无法修复它。伸出援手是最后的办法。
请参阅下面的代码以及回溯:
!pip install geopandas
!pip install geopy
!pip install folium
import pandas as pd
import folium
import
from functools import reduce
from io import BytesIO
import requests
import os
import geopandas as gpd
LA_map = folium.Map(location= [34.052235, -118.243683], zoom_start= 10)
df_geojson = gpd.read_file(
r'https://raw.githubusercontent.com/tzick90/datasources/main/map.geojson'
)
LA_zipcodes = df_geojson['zipcode'].tolist()
CA_househould_income = '1Gfa2sG0SzDdgV9bztVZvZh8U9ti0ei_BpZr3swGY3mg'
CA_househould_income_file = f'https://docs.google.com/spreadsheets/d/{CA_househould_income}/export?format=csv'
r2 = requests.get(CA_househould_income_file)
CA_HI = pd.read_csv(BytesIO(r2.content))
LA_avg_income = CA_HI['zip_code'].isin(LA_zipcodes)
LA_avg_income_clean = CA_HI[LA_avg_income].reset_index()
LA_avg_income_clean.rename(columns = {'zip_code':'zipcode'}, inplace= True)
LA_avg_income_clean['zipcode'] = LA_avg_income_clean['zipcode'].astype('str')
LA_avg_income_clean_list = LA_avg_income_clean['zipcode'].tolist()
LA_zipcode_clean = df_geojson['zipcode'].isin(LA_avg_income_clean_list)
LA_zipcode_clean_final = df_geojson[LA_zipcode_clean].reset_index()
LA_zipcode_clean_final['zipcode_'] = LA_zipcode_clean_final['zipcode']
LA_avg_income_clean['zipcode_'] = LA_avg_income_clean['zipcode']
LA_avg_income_clean['zipcode'] = LA_avg_income_clean['zipcode'].astype('str')
LA_zipcode_clean_final1 = LA_zipcode_clean_final.sort_values(by= 'zipcode', ascending = True).set_index('zipcode_')
LA_avg_income_clean1 = LA_avg_income_clean.sort_values(by= 'zipcode', ascending = True).set_index('zipcode_')
zip_boundries1 = LA_zipcode_clean_final1.to_json()
folium.Choropleth(
geo_data= zip_boundries1,
name= 'choropleth',
data= LA_avg_income_clean1,
columns= ['zipcode','Avg. Income/H/hold'],
key_on= 'feature.properties.zipcode',
fill_color= 'YlGn',
#nan_fill_opacity= 0.1,
fill_opacity=0.3,
line_opacity=0.9,
legend_name= "Average Income per Household in USD",
).add_to(LA_map)
display(LA_map)
以下是我几乎经常收到的错误消息:
TypeError Traceback (most recent call last)
<ipython-input-185-62a5660e5e7c> in <module>
----> 1 folium.Choropleth(
2 geo_data= zip_boundries1,
3 name= 'choropleth',
4 data= LA_avg_income_clean1,
5 columns= ['zipcode','Avg. Income/H/hold'],
~/opt/anaconda3/lib/python3.8/site-packages/folium/features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
1211 if color_data is not None and key_on is not None:
1212 real_values = np.array(list(color_data.values()))
-> 1213 real_values = real_values[~np.isnan(real_values)]
1214 _, bin_edges = np.histogram(real_values, bins=bins)
1215
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
有任何如何修复/解决错误的建议吗? 非常感谢您提出的任何建议或解决方案。
致以最良好的问候
推荐答案
我们简化了您的任务并创建了代码。挑战在于,每个数据中的邮政编码必须是一个字符串,否则Folium将不支持它。此外,邮政编码区域中表示的值是美元表示法的字符串,因此需要将它们转换为数字。
import pandas as pd
import folium
from io import BytesIO
import requests
import geopandas as gpd
from re import sub
from decimal import Decimal
df_geojson = gpd.read_file(r'https://raw.githubusercontent.com/tzick90/datasources/main/map.geojson')
LA_zipcodes = df_geojson['zipcode'].tolist()
df_geojson['zipcode'] = df_geojson['zipcode'].astype(str)
CA_househould_income = '1Gfa2sG0SzDdgV9bztVZvZh8U9ti0ei_BpZr3swGY3mg'
CA_househould_income_file = f'https://docs.google.com/spreadsheets/d/{CA_househould_income}/export?format=csv'
r2 = requests.get(CA_househould_income_file)
CA_HI = pd.read_csv(BytesIO(r2.content))
CA_HI.rename(columns = {'zip_code':'zipcode'}, inplace= True)
CA_HI['zipcode'] = CA_HI['zipcode'].astype(str)
CA_HI['Avg. Income/H/hold'] = CA_HI['Avg. Income/H/hold'].apply(lambda x: Decimal(sub(r'[^d.]', '', x)))
CA_HI['Avg. Income/H/hold'] = CA_HI['Avg. Income/H/hold'].astype(int)
LA_map = folium.Map(location= [34.052235, -118.243683], zoom_start= 10)
folium.Choropleth(
geo_data= df_geojson.to_json(),
name= 'choropleth',
data= CA_HI,
columns= ['zipcode','Avg. Income/H/hold'],
key_on= 'feature.properties.zipcode',
fill_color= 'YlGn',
#nan_fill_opacity= 0.1,
fill_opacity=0.3,
line_opacity=0.9,
legend_name= "Average Income per Household in USD",
).add_to(LA_map)
display(LA_map)
这篇关于不支持Python/Folium/clopeth:TypeError:ufunc&39;isnan;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!