类型错误:不支持ufunc 'isnan' - python,numpy,pandas,folium



我正试图使用folium创建Choropleth地图,但在这个过程中,我遇到了以下错误消息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_21004/1239111326.py in <module>
----> 1 folium.Choropleth(
2     geo_data = json,
3     name = "choropleth",
4     data = df,
5     columns = ["state_code", "men"],
~AppDataLocalProgramsPythonPython39libsite-packagesfoliumfeatures.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''

这是我的代码,你可以在这里找到table.csv和map.geojson文件-https://wetransfer.com/downloads/206aca1bf6712bfb49e1ce5979da2be220211104093237/204e9a:

import folium
import pandas as pd
json = f"map.geojson"
map = folium.Map(location = [42.688534, 25.280583], zoom_start = 7)
table = f"table2.csv"
df = pd.read_csv(table, encoding= 'unicode_escape')
folium.Choropleth(
geo_data = json,
name = "choropleth",
data = df,
columns = ["state_code", "men"],
key_on = "feature.properties.state_code",
fill_color = "BuPu",
fill_opacity= 0.7,
line_opacity= 0.2,
legend_name="Men population(n)"
).add_to(map)
map

我发现这个问题(Python/Folium/Choropleth:TypeError:ufunc';不支持(与我的问题类似,但我仍然无法找到/应用解决方案。请让我知道我做错了什么。

我设法找到了错误的解决方案。不得不删除表文件中数字之间的间距,现在它可以工作了。

最新更新