本文将分享Pythonpandas入门05JSON的详细内容,并且还将对pandasjson_normal进行详尽解释,此外,我们还将为大家带来关于/opt/conda/lib/python3.6/s
本文将分享Python pandas 入门 05 JSON的详细内容,并且还将对pandas json_normal进行详尽解释,此外,我们还将为大家带来关于/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN、100天精通Python(数据分析篇)——第59天:Pandas读写json文件(read_json、to_json)、GeoPandas入门 | 05-Python可视化空间数据、pandas – Python folium GeoJSON地图不显示的相关知识,希望对你有所帮助。
本文目录一览:- Python pandas 入门 05 JSON(pandas json_normal)
- /opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN
- 100天精通Python(数据分析篇)——第59天:Pandas读写json文件(read_json、to_json)
- GeoPandas入门 | 05-Python可视化空间数据
- pandas – Python folium GeoJSON地图不显示
Python pandas 入门 05 JSON(pandas json_normal)
Pandas JSON
JSON(JavaScript Object Notation,JavaScript 对象表示法),是存储和交换文本信息的语法,类似 XML。
JSON 比 XML 更小、更快,更易解析,更多 JSON 内容可以参考 JSON 教程。
Pandas 可以很方便的处理 JSON 数据,本文以 sites.json 为例,内容如下:
实例
[ { "id": "A001", "name": "教程", "url": "www.run.com", "likes": 61 }, { "id": "A002", "name": "Google", "url": "www.google.com", "likes": 124 }, { "id": "A003", "name": "淘宝", "url": "www.taobao.com", "likes": 45 } ]
实例
df = pd.read_json('sites.json')
print(df.to_string())
to_string() 用于返回 DataFrame 类型的数据,我们也可以直接处理 JSON 字符串。
实例
data =[
{
"id": "A001",
"name": "教程",
"url": "www.run.com",
"likes": 61
},
{
"id": "A002",
"name": "Google",
"url": "www.google.com",
"likes": 124
},
{
"id": "A003",
"name": "淘宝",
"url": "www.taobao.com",
"likes": 45
}
]
df = pd.DataFrame(data)
print(df)
以上实例输出结果为:
id name url likes
0 A001 教程 www.run.com 61
1 A002 Google www.google.com 124
2 A003 淘宝 www.taobao.com 45
JSON 对象与 Python 字典具有相同的格式,所以我们可以直接将 Python 字典转化为 DataFrame 数据:
实例
# 字典格式的 JSON
s = {
"col1":{"row1":1,"row2":2,"row3":3},
"col2":{"row1":"x","row2":"y","row3":"z"}
}
# 读取 JSON 转为 DataFrame
df = pd.DataFrame(s)
print(df)
以上实例输出结果为:
col1 col2
row1 1 x
row2 2 y
row3 3 z
从 URL 中读取 JSON 数据:
实例
URL = 'https://static.runoob.com/download/sites.json'
df = pd.read_json(URL)
print(df)
以上实例输出结果为:
id name url likes
0 A001 教程 www.run.com 61
1 A002 Google www.google.com 124
2 A003 淘宝 www.taobao.com 45
内嵌的 JSON 数据
假设有一组内嵌的 JSON 数据文件 nested_list.json :
nested_list.json 文件内容
"school_name": "ABC primary school",
"class": "Year 1",
"students": [
{
"id": "A001",
"name": "Tom",
"math": 60,
"physics": 66,
"chemistry": 61
},
{
"id": "A002",
"name": "James",
"math": 89,
"physics": 76,
"chemistry": 51
},
{
"id": "A003",
"name": "Jenny",
"math": 79,
"physics": 90,
"chemistry": 78
}]
}
使用以下代码格式化完整内容:
实例
df = pd.read_json('nested_list.json')
print(df)
以上实例输出结果为:
school_name class students
0 ABC primary school Year 1 {'id': 'A001', 'name': 'Tom', 'math': 60, 'phy...
1 ABC primary school Year 1 {'id': 'A002', 'name': 'James', 'math': 89, 'p...
2 ABC primary school Year 1 {'id': 'A003', 'name': 'Jenny', 'math': 79, 'p...
这时我们就需要使用到 json_normalize() 方法将内嵌的数据完整的解析出来:
实例
import json
# 使用 Python JSON 模块载入数据
with open('nested_list.json','r') as f:
data = json.loads(f.read())
# 展平数据
df_nested_list = pd.json_normalize(data, record_path =['students'])
print(df_nested_list)
以上实例输出结果为:
id name math physics chemistry
0 A001 Tom 60 66 61
1 A002 James 89 76 51
2 A003 Jenny 79 90 78
data = json.loads(f.read()) 使用 Python JSON 模块载入数据。
json_normalize() 使用了参数 record_path 并设置为 ['students'] 用于展开内嵌的 JSON 数据 students。
显示结果还没有包含 school_name 和 class 元素,如果需要展示出来可以使用 Meta 参数来显示这些元数据:
实例
import json
# 使用 Python JSON 模块载入数据
with open('nested_list.json','r') as f:
data = json.loads(f.read())
# 展平数据
df_nested_list = pd.json_normalize(
data,
record_path =['students'],
Meta=['school_name', 'class']
)
print(df_nested_list)
以上实例输出结果为:
id name math physics chemistry school_name class
0 A001 Tom 60 66 61 ABC primary school Year 1
1 A002 James 89 76 51 ABC primary school Year 1
2 A003 Jenny 79 90 78 ABC primary school Year 1
接下来,让我们尝试读取更复杂的 JSON 数据,该数据嵌套了列表和字典,数据文件 nested_mix.json 如下:
nested_mix.json 文件内容
"school_name": "local primary school",
"class": "Year 1",
"info": {
"president": "John Kasich",
"address": "ABC road, London, UK",
"contacts": {
"email": "admin@e.com",
"tel": "123456789"
}
},
"students": [
{
"id": "A001",
"name": "Tom",
"math": 60,
"physics": 66,
"chemistry": 61
},
{
"id": "A002",
"name": "James",
"math": 89,
"physics": 76,
"chemistry": 51
},
{
"id": "A003",
"name": "Jenny",
"math": 79,
"physics": 90,
"chemistry": 78
}]
}
nested_mix.json 文件转换为 DataFrame:
实例
import json
# 使用 Python JSON 模块载入数据
with open('nested_mix.json','r') as f:
data = json.loads(f.read())
df = pd.json_normalize(
data,
record_path =['students'],
Meta=[
'class',
['info', 'president'],
['info', 'contacts', 'tel']
]
)
print(df)
以上实例输出结果为:
id name math physics chemistry class info.president info.contacts.tel
0 A001 Tom 60 66 61 Year 1 John Kasich 123456789
1 A002 James 89 76 51 Year 1 John Kasich 123456789
2 A003 Jenny 79 90 78 Year 1 John Kasich 123456789
读取内嵌数据中的一组数据
以下是实例文件 nested_deep.json,我们只读取内嵌中的 math 字段:
nested_deep.json 文件内容
"school_name": "local primary school",
"class": "Year 1",
"students": [
{
"id": "A001",
"name": "Tom",
"grade": {
"math": 60,
"physics": 66,
"chemistry": 61
}
},
{
"id": "A002",
"name": "James",
"grade": {
"math": 89,
"physics": 76,
"chemistry": 51
}
},
{
"id": "A003",
"name": "Jenny",
"grade": {
"math": 79,
"physics": 90,
"chemistry": 78
}
}]
}
这里我们需要使用到 glom 模块来处理数据套嵌,glom 模块允许我们使用 . 来访问内嵌对象的属性。
第一次使用我们需要安装 glom:
pip3 install glom
实例
from glom import glom
df = pd.read_json('nested_deep.json')
data = df['students'].apply(lambda row: glom(row, 'grade.math'))
print(data)
以上实例输出结果为:
0 60
1 89
2 79
Name: students, dtype: int64
REF
https://www.runoob.com/pandas/pandas-json.html
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN
这里记录一下犯过的及其傻帽的错误!!!!哈哈,无语,同时讨论一下NaN这个数据类型的处理
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison result = getattr(x,name)(y)
....................
TypeError: invalid type comparison
这里有一个优惠券的scv表:
import numpy as np
import pandas as pd
dfoff = pd.read_csv("datalab/4901/ccf_offline_stage1_train.csv")
dfofftest = pd.read_csv("datalab/4901/ccf_offline_stage1_test_revised.csv")
dfoff.head()
笔者这里的目的是想统计出 Coupon_id是非NaN(非空)且Date是NaN(空)的用户数(行数)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
一般来说比如我们想筛选出 discount_rate是20:1且distance不是1.0的行数可以这么做:
dfoff.info()
print('数目是:',dfoff[(dfoff['discount_rate']=='20:1')&(dfoff['Date']!=1.0)].shape[0])
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
于是笔者这样做了筛选:
dfoff.info()
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']!='NaN')&(dfoff['Date']=='NaN')].shape[0])
结果报错:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1754884 entries,0 to 1754883
Data columns (total 7 columns):
User_id int64
Merchant_id int64
Coupon_id float64
discount_rate object
distance float64
Date_received float64
Date float64
dtypes: float64(4),int64(2),object(1)
memory usage: 93.7+ MB
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: FutureWarning: elementwise comparison Failed; returning scalar instead,but in the future will perform elementwise comparison
result = getattr(x,name)(y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-c27c94978405> in <module>()
1 dfoff.info()
----> 2 print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']!='NaN')&(dfoff['Date']=='NaN')].shape[0])
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(self,other,axis)
877
878 with np.errstate(all='ignore'):
--> 879 res = na_op(values,other)
880 if is_scalar(res):
881 raise TypeError('Could not compare {typ} type with Series'
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x,y)
816 result = getattr(x,name)(y)
817 if result is NotImplemented:
--> 818 raise TypeError("invalid type comparison")
819 except AttributeError:
820 result = op(x,y)
TypeError: invalid type comparison
其实吧原因很简单,注意看上面笔者故意标红的地方,Coupon_id 和Date的数据类型都是float64,而代码中却用了dfoff['Coupon_id']!='NaN',这不是字符串嘛!!!!!!
print(type('NaN'))
<class 'str'>
float和str比较当然报错了是吧,哎!能这样直接去比较我也算是极品啦哈哈哈
于是可以使用其内置的方法解决:
dfoff.info()
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id'].notnull())&(dfoff['Date'].isnull())].shape[0])
即使用了如下两个方法
.notnull()
.isnull()
其作用就是判断是否是空值,如果csv中的NaN的地方换成null同样适用
同时这里说一下怎么将NaN替换掉:例如替换成0.0
dfoff['Coupon_id']=dfoff['Coupon_id'].replace(np.nan,0.0)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
下面来说一下NaN这个数据类型,它的全称应该是not a number,说到这里不得不提到另外一个数据类型inf
相同点:都是代表一个无法表示的数
不同点:inf代表无穷大,是一个超过浮点表示范围的浮点数,而NaN可以看成是缺少值或者是无理数
假设现在有一段程序:
def ConvertRate(row):
if row.isnull():
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
会发现报错:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-0aa06185ee75> in <module>()
7 else:
8 return float(row)
----> 9 dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
10 print(dfoff.head(3))
/opt/conda/lib/python3.6/site-packages/pandas/core/series.py in apply(self,func,convert_dtype,args,**kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values,f,convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0],Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-3-0aa06185ee75> in ConvertRate(row)
1 def ConvertRate(row):
----> 2 if row.isnull():
3 return 0
4 elif ':' in str(row):
5 rows = str(row).split(':')
AttributeError: 'float' object has no attribute 'isnull'
那它到底是什么数据类型呢?
print(type(np.nan))
print(type(np.inf))
<class 'float'>
<class 'float'>
NaN'就是表示一个普通的字符串,而np.nan就是代表真真的nan,那我们可不可以使用这样:
def ConvertRate(row):
if row==np.nan:
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
User_id Merchant_id Coupon_id discount_rate distance Date_received \
0 1439408 2632 NaN NaN 0.0 NaN
1 1439408 4663 11002.0 150:20 1.0 20160528.0
2 1439408 2632 8591.0 20:1 0.0 20160217.0
Date discount_rate
0 20160217.0 NaN
1 NaN 0.866667
2 NaN 0.950000
可以看到这里还是NaN,并不是0,说明还是不对
那试一下:
def ConvertRate(row):
if row==float('NaN'):
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
结果还是如上面,其实NaN数据类型就是一种特殊的float,这里相当于强制类型转化
那到底怎么办呢?其实判断是否是NaN可以使用如下方法:
row!=row
如果结果是真,那么就是NaN,假就代表不是NaN
可以看一下结果:
def ConvertRate(row):
if row!=row:
return 0
elif ':' in str(row):
rows = str(row).split(':')
return 1.0-float(rows[1])/float(rows[0])
else:
return float(row)
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
print(dfoff.head(3))
print(dfoff.head(3))
User_id Merchant_id Coupon_id discount_rate distance Date_received \
0 1439408 2632 NaN NaN 0.0 NaN
1 1439408 4663 11002.0 150:20 1.0 20160528.0
2 1439408 2632 8591.0 20:1 0.0 20160217.0
Date discount_rate
0 20160217.0 0.000000
1 NaN 0.866667
2 NaN 0.950000
于是笔者最开始的那个问题也可以这样解决:
print('有优惠券,但是没有使用优惠券购买的客户有',dfoff[(dfoff['Coupon_id']==dfoff['Coupon_id'])&(dfoff['Date']!=dfoff['Date'])].shape[0])
有优惠券,但是没有使用优惠券购买的客户有 977900
---------------------------------------------------------------------------------------------------------------------------------------------------------------
有时候在使用apply的时候会报错,所以最好加一下:axis = 1意思是按列处理的
对应到上面就是吧:
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate)
改为:
dfoff['discount_rate'] = dfoff['discount_rate'].apply(ConvertRate,axis = 1)
------------------------------------------------------------------------------------------------------------------------------------------------------------
所以最后总结一下:
NaN和inf都是一种特殊的float数据类型
可以使用row!=row类似的形式来判断是否是NaN,如果是真就代表是NaN,假就代表不是NaN,换句话说也可以使用row==row来判断是否是NaN,只不过逻辑相反而已
报错记得加axis = 1
------------------------------------------------------------------------------------------------------------------------------------------------------
在使用pands加载数据的时候,其实我们是可以控制数据类型的,比如让缺省值变为null,而不是NAN,即让字段的数据类型不再是float,而是object,这里有一个例子:https://blog.csdn.net/weixin_42001089/article/details/85013073
100天精通Python(数据分析篇)——第59天:Pandas读写json文件(read_json、to_json)
文章目录
- 每篇前言
- 一、read_json()
-
- 1. path_or_buf
- 2. orient
- 3. typ
- 4. dtype
- 5. convert_axes
- 6. convert_dates
- 7. keep_default_dates
- 8. numpy
- 9. precise_float
- 10. date_unit
- 11. lines
- 12. encoding
- 二、to_json()
-
- 1. path_or_buf
GeoPandas入门 | 05-Python可视化空间数据
05-Python可视化空间数据
源代码请看此处
%matplotlib inline
import pandas as pd
import geopandas
import matplotlib.pyplot as plt
countries = geopandas.read_file("zip://data/ne_110m_admin_0_countries.zip")
cities = geopandas.read_file("zip://data/ne_110m_populated_places.zip")
rivers = geopandas.read_file("zip://data/ne_50m_rivers_lake_centerlines.zip")
5.1 GeoPandas的可视化函数
基础绘图
countries.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f7e787c93c8>
调整地图大小
countries.plot(figsize=(15,15))
<matplotlib.axes._subplots.AxesSubplot at 0x7f7e764cccc0>
移除边框及x,y坐标
ax=countries.plot(figsize=(15,15))
ax.set_axis_off()
根据列值进行着色
首先创建一个新的人均国内生产总值列
countries=countries[(countries[''pop_est'']>0)&(countries[''name'']!="Antarctica")]
countries[''gdp_per_cap''] = countries[''gdp_md_est''] / countries[''pop_est''] * 100
现在可以使用这个列来给多边形着色:
ax = countries.plot(figsize=(15, 15), column=''gdp_per_cap'')
ax.set_axis_off()
ax = countries.plot(figsize=(15, 15), column=''gdp_per_cap'', scheme=''quantiles'', legend=True) # scheme 分级方法
ax.set_axis_off()
将多个GeoDataframe的数据同时可视化
.plot
方法返回一个matplotlib的Axes对象,然后可以通过ax=
关键字重新使用这个对象为该图添加额外的图层
ax=countries.plot(figsize=(15,15))
cities.plot(ax=ax,color=''red'',markersize=10)
ax.set_axis_off()
ax=countries.plot(edgecolor=''k'',facecolor=''none'',figsize=(15,15))
rivers.plot(ax=ax)
cities.plot(ax=ax,color=''C1'')
# ax.set(xlim=(70,135),ylim=(0,55))
ax.set_axis_off()
使用contextily
添加背景图
该包允许轻松地将基于Web-tile的背景图(basemap)添加到的GeoPandas图中
目前,唯一的要求是数据投影为WebMercator
(EPSG:3857)
cities_europe = cities[cities.within(countries[countries[''continent''] == ''Europe''].unary_union)]
# .unary_union: Returns a geometry containing the union of all geometries in the GeoSeries
cities_europe2 = cities_europe.to_crs(epsg=3857)
ax=cities_europe2.plot()
import contextily as ctx
ax=cities_europe2.plot(figsize=(10,6))
ctx.add_basemap(ax)
ax = cities_europe2.plot(figsize=(10, 6))
ctx.add_basemap(ax, source=ctx.providers.Stamen.TonerLite)
5.2 使用geoplot
包进行可视化
与GeoDataFrames的基本.plot()
方法相比,geoplot
包提供了一些额外的功能
- 高级绘图API(有更多的绘图类型)
- 通过cartopy支持本地投影
https://residentmario.github.io/geoplot/index.html
# ! pip3 install geoplot
import sys
import geoplot
import geoplot.crs as gcrs
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={
''projection'': gcrs.Orthographic(central_latitude=35, central_longitude=116.0059)
})
geoplot.choropleth(countries, hue=''gdp_per_cap'', projection=gcrs.Orthographic(), ax=ax,
cmap=''BuGn'', linewidth=1, edgecolor=''white'')
ax.set_global()
ax.outline_patch.set_visible(True)
# ax.coastlines()
# Geometry must be a Point or LineString
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: DeprecationWarning: The outline_patch property is deprecated. Use GeoAxes.spines[''geo''] or the default Axes properties instead.
import sys
5.3 使用cartopy
包进行可视化
使用cartopy
Cartopy是matplotlib的基础制图库,它被geoplot
用来提供投影
http://scitools.org.uk/cartopy/docs/latest/index.html
下面的例子取自文档:http://geopandas.readthedocs.io/en/latest/gallery/cartopy_convert.html#sphx-glr-gallery-cartopy-convert-py 。
from cartopy import crs as ccrs
crs=ccrs.AlbersEqualArea()
crs_proj4=crs.proj4_init
countries_ae=countries.to_crs(crs_proj4)
countries_ae.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f7e56a852b0>
fig,ax=plt.subplots(subplot_kw={''projection'':crs})
ax.add_geometries(countries_ae[''geometry''],crs=crs)
<cartopy.mpl.feature_artist.FeatureArtist at 0x7f7e569b1080>
fig, ax = plt.subplots(subplot_kw={''projection'': crs})
countries_ae[''geometry''].plot(ax=ax)
<cartopy.mpl.geoaxes.GeoAxesSubplot at 0x7f7e568c34a8>
5.4 基于web的交互式可视化
现在有很多针对交互式网络可视化的库,可以处理地理空间数据:
- Bokeh: https://bokeh.pydata.org/en/latest/docs/gallery/texas.html
- GeoViews (other interface to Bokeh/matplotlib): http://geo.holoviews.org
- Altair: https://altair-viz.github.io/gallery/choropleth.html
- Plotly: https://plot.ly/python/#maps
- ...
另一个流行的在线地图的javascript库是Leaflet.js,这个库在folium和ipyleaflet包中有python接口
ipyleaflet
的使用实例:
# ! pip3 install ipyleaflet
import ipyleaflet
m = ipyleaflet.Map(center=[48.8566, 2.3429], zoom=3)
geo_data = ipyleaflet.GeoData(
geo_dataframe = countries,
style={''color'': ''black'', ''fillColor'': ''#3366cc'', ''opacity'':0.05, ''weight'':1.9, ''dashArray'':''2'', ''fillOpacity'':0.6},
hover_style={''fillColor'': ''red'' , ''fillOpacity'': 0.2},
name = ''Countries'')
m.add_layer(geo_data)
m
# jupyter labextension install @jupyter-widgets/jupyterlab-manager
Map(center=[48.8566, 2.3429], controls=(ZoomControl(options=[''position'', ''zoom_in_text'', ''zoom_in_title'', ''zoo…
更多信息:https://ipyleaflet.readthedocs.io/en/latest/api_reference/geodata.html
folium
的使用实例
import folium
m = folium.Map([39, 116], zoom_start=6, tiles="OpenStreetMap")
folium.GeoJson(countries).add_to(m)
folium.GeoJson(cities).add_to(m)
m
pandas – Python folium GeoJSON地图不显示
我正在尝试使用geopandas,pandas和folium的组合来创建一个我可以嵌入到网页中的多边形地图.
出于某种原因,它没有显示并想知道是否有人可以提供帮助.
我采取的步骤:
1)从英国的议会边界操作系统中获取.shp.
2)然后我使用geopandas将投影更改为epsg = 4326,然后导出为GeoJSON,采用以下格式:
{ "type": "Feature","properties": { "PCON13CD": "E14000532","PCON13CDO": "A03","PCON13NM": "Altrincham and Sale West" },"geometry": { "type": "polygon","coordinates": [ [ [ -2.313999519326579,53.357408280545918 ],[ -2.313941776174758,53.358341455420039 ],[ -2.31519699483377,53.359035664493433 ],[ -2.317953152796459,53.359102954309151 ],[ -2.319855973429864,53.358581917200119 ],... ] ] ] } },...
那么我想要做的是将其与以下格式的选区的数据框进行网格化,dty:
constituency count 0 Burton 667 1 Cannock Chase 595 2 Cheltenham 22 3 Cheshire East 2 4 Congleton 1 5 Derbyshire Dales 1 6 East Staffordshire 4 import folium mapf = folium.Map(width=700,height=370,tiles = "Stamen Toner",zoom_start=8,location= ["53.0219392","-2.1597434"]) mapf.geo_json(geo_path="geo_json_shape2.json",data_out="data.json",data=dty,columns=["constituency","count"],key_on="feature.properties.PCON13NM.geometry.type.polygon",fill_color=''PuRd'',fill_opacity=0.7,line_opacity=0.2,reset="True")
mapf的输出如下:
mapf.json_data
{''../../Crime_data/staffs_data92.json'': [{''Burton'': 667,''Cannock Chase'': 595,''Cheltenham'': 22,''Cheshire East'': 2,''Congleton'': 1,''Derbyshire Dales'': 1,''East Staffordshire'': 4,''Lichfield'': 438,''Newcastle-under-Lyme'': 543,''north Warwickshire'': 1,''Shropshire'': 17,''South Staffordshire'': 358,''Stafford'': 623,''Staffordshire Moorlands'': 359,''Stoke-on-Trent Central'': 1053,''Stoke-on-Trent north'': 921,''Stoke-on-Trent South'': 766,''Stone'': 270,''Tamworth'': 600,''Walsall'': 1}]}
虽然mapf.create_map()函数成功创建了一个地图,但多边形不会渲染.
有谁能建议任何调试步骤?
如果有人需要,我永远不清楚如何添加完整的数据文件,所以请告诉我.
感谢您提前的时间.
解决方法
pip install mplleaflet
在Jupyter / Ipython笔记本中:
import mplleaflet ax = geopandas_df.plot(column=''variable_to_plot'',scheme=''QUANTILES'',k=9,colormap=''YlOrRd'') mplleaflet.show(fig=ax.figure)
关于Python pandas 入门 05 JSON和pandas json_normal的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py:816: pandas 处理 NaN、100天精通Python(数据分析篇)——第59天:Pandas读写json文件(read_json、to_json)、GeoPandas入门 | 05-Python可视化空间数据、pandas – Python folium GeoJSON地图不显示的相关信息,请在本站寻找。
本文标签: