Dataframe 对象在 geopandas 中不可调用

Dataframe object is not callable in geopandas

我有一个包含纬度经度温度的数据集 我试着在世界地图上根据经纬度绘制温度等高线图。我还需要在地图中标记等高线间隔。 我尝试了从类似问题中获得的以下代码。

import numpy as np
from scipy.interpolate import griddata
from matplotlib import pyplot as plt
import geojsoncontour
Import pandas as pd
import geopandas as gpd

Data=pd.read_csv("df.txt")
Data1=Dataframe.to_csv('sample.csv', index=None)
Sample=pd read_csv('sample.csv',delim_whitspace=True)
temp = sample['temp']
lon = sample['lon']
lat = sample['lat']

y = np.linspace(min(lon), max(lon), 100)
x = np.linspace(min(lat), max(lat), 100)

X,Y = np.meshgrid(x, y)
Z = griddata((lat, lon), temp, (X, Y), method='cubic')
    
contour =    plt.contour(X,Y, Z)

gdf = gpd.GeoDataFrame.from_features(sample(geojsoncontour.contour_to_geojson(
    contour=contour,
    min_angle_deg=3.0,
    ndigits=5,
    stroke_width=1))).set_crs("EPSG:4326")

m = gdf.explore(color=gdf["stroke"])

plt.show()
m

我收到错误“Dataframe 对象不可调用” 我该如何解决这个问题!!

您的代码存在多个语法和语义错误。请参阅下面固定代码中的注释。

基本上错误Dataframe object is not callable 使用 dataframe 对象将 JSON 编码字符串转换为 JSON 是没有意义的框架对象。显然你想使用 https://docs.python.org/3/library/json.html

import numpy as np
from scipy.interpolate import griddata
from matplotlib import pyplot as plt
import geojsoncontour
# Import pandas as pd # import with a capital !!!
import pandas as pd
import geopandas as gpd
import json

# let's create a file as it doesn't exist on any system other than OP
with open("df.txt", "w") as f:
    f.write("""lat,lon,temp
34.355,141.6211,11.116286876289658
-17.7691,-178.312,7.952002019397204
-6.5982,147.3301,24.62322868970752
50.954,-179.2585,11.71324100350295
11.9822,-87.1375,15.01789103393663
-33.2708,-71.7855,24.469164363171046
-36.8458,-74.1843,18.256370193874996
32.602,-40.1483,7.677618759312514
13.9702,120.821,21.65741634737647
35.0176667,-118.118,9.308472581594794
-6.038,148.5685,7.303807691740786
-10.9269,162.1543,20.834742114943694
-22.4121,-67.3461,22.421598068428683
-5.8357,128.9122,23.48156402329026
61.3715,-150.045,13.103182072669588
51.7784,-173.9138,7.828683632233058
-2.5579,99.5117,12.704418487583837
-14.4739,-13.3795,21.39126818102271
18.697,-68.0025,19.162216173098656
36.2098333,-117.856,8.162743541290157""")

Data=pd.read_csv("df.txt")
## very odd
# Data1=Dataframe.to_csv('sample.csv', index=None)
Data1=Data.to_csv('sample.csv', index=None)
# Sample=pd read_csv('sample.csv',delim_whitspace=True) 
## 1. pd<space>read_csv!!!
## 2. unpercase Sample when following lines are lowercase
## 3. invalid parameter delim_whitspace=True
sample=pd.read_csv('sample.csv')
# temp = pd.cut(sample['temp'], bins=5, labels=range(5)).astype(int)
temp = sample["temp"]
lon = sample['lon']
lat = sample['lat']

y = np.linspace(min(lon), max(lon), 100)
x = np.linspace(min(lat), max(lat), 100)

X,Y = np.meshgrid(x, y)
Z = griddata((lat, lon), temp, (X, Y), method='cubic')
    
contour =    plt.contour(X,Y, Z)

## sample is a dataframe object, so why would you use it to convert a string encoding of JSON to JSON?
# gdf = gpd.GeoDataFrame.from_features(sample(geojsoncontour.contour_to_geojson(
#     contour=contour,
#     min_angle_deg=3.0,
#     ndigits=5,
#     stroke_width=1))).set_crs("EPSG:4326")
gdf = gpd.GeoDataFrame.from_features(json.loads(geojsoncontour.contour_to_geojson(
    contour=contour,
    min_angle_deg=3.0,
    ndigits=5,
    stroke_width=1))).set_crs("EPSG:4326")


m = gdf.explore(color=gdf["stroke"])

plt.show()
m

在选项卡中打开地图

你没有说明你的环境。 Python: How to open map created with folium in browser when running application

如果您在 Jupyter 中 运行,地图将刚刚显示。

from pathlib import Path
import webbrowser
f = Path.cwd().joinpath("map.html")
m.save(str(f))
webbrowser.open(str(f), new=2)

间隔

假设你的意思是垃圾桶.... https://pandas.pydata.org/docs/reference/api/pandas.cut.html

因此,如果您只想考虑 5 个温度带

temp = pd.cut(sample['temp'], bins=5, labels=range(5)).astype(int)