简体   繁体   中英

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. I need to mark the contour line intervals also in the 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!!

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. Clearly you want to use 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

You have not stated you environment. Python: How to open map created with folium in browser when running application

If you are running in Jupyter, the map will just have displayed.

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

Hence if you just want to consider 5 bands of temperature

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM