簡體   English   中英

Dataframe object 在 geopandas 中不可調用

[英]Dataframe object is not callable in geopandas

我有一個數據集包含緯度經度 Temp 我試圖根據世界 map 上的緯度和經度繪制溫度的輪廓 map。 我還需要在 map 中標記等高線間隔。 我嘗試了從類似問題中獲得的以下代碼。

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 object 不可調用”我該如何解決這個問題!!

您的代碼有多個語法和語義錯誤。 請參閱下面固定代碼中的注釋。

Fundamentally the error Dataframe object is not callable Makes no sense to use a dataframe object to convert a JSON encoded string to JSON using an data frame object. 顯然你想使用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

在選項卡中打開 map

你沒有說明你的環境。 Python:如何在運行應用程序時在瀏覽器中打開使用 folium 創建的 map

如果您在 Jupyter 中運行,則 map 將剛剛顯示。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM