简体   繁体   English

Dataframe object 在 geopandas 中不可调用

[英]Dataframe object is not callable in geopandas

I have a dataset contains latitude longitude Temp I tried to draw a contour map of temp based on latitude and longitude on the world map.我有一个数据集包含纬度经度 Temp 我试图根据世界 map 上的纬度和经度绘制温度的轮廓 map。 I need to mark the contour line intervals also in the map.我还需要在 map 中标记等高线间隔。 I tried the below code got from a similar question.我尝试了从类似问题中获得的以下代码。

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

I'm getting an error ' Dataframe object is not callable' How can I solve the issue!!我收到一个错误“Dataframe object 不可调用”我该如何解决这个问题!!

Your code has multiple syntax and semantic errors.您的代码有多个语法和语义错误。 See comments in fixed code below.请参阅下面固定代码中的注释。

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. 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. Clearly you want to use https://docs.python.org/3/library/json.html显然你想使用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

open map in tab在选项卡中打开 map

You have not stated you environment.你没有说明你的环境。 Python: How to open map created with folium in browser when running application Python:如何在运行应用程序时在浏览器中打开使用 folium 创建的 map

If you are running in Jupyter, the map will just have displayed.如果您在 Jupyter 中运行,则 map 将刚刚显示。

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

intervals间隔

assume you mean bins.... https://pandas.pydata.org/docs/reference/api/pandas.cut.html假设你的意思是垃圾箱.... https://pandas.pydata.org/docs/reference/api/pandas.cut.html

Hence if you just want to consider 5 bands of temperature因此,如果您只想考虑 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