heat_data = [[row['start_lat'], row['start_lng']] for index, row in df.iterrows()]
时间: 2023-10-26 07:05:14 浏览: 31
这段代码是将一个 pandas DataFrame 中的经纬度数据提取出来,组成一个二维列表 heat_data。其中,iterrows() 方法可以遍历 DataFrame 中的每一行数据,row['start_lat'] 和 row['start_lng'] 则是获取每行数据中的经纬度信息。最终,heat_data 是一个由每行数据的经纬度信息组成的二维列表。
相关问题
temp_df = pd.DataFrame(columns=['year', 'month', 'city', 'temperature']) for year in years: for month in months: temp_array = temp_data[(year-1979)*12+month-1, :, :] for i, row in shp_file.iterrows(): city_name = row['市'] city_geom = row['geometry'] temp_mean = np.mean(temp_array[city_geom.contains_points(temp_array.lon, temp_array.lat)]) temp_df = temp_df.append({'year': year, 'month': month, 'city': city_name, 'temperature': temp_mean}, ignore_index=True)这串代码报错 temp_mean = np.mean(temp_array[city_geom.contains_points(temp_array.lon, temp_array.lat)]) AttributeError: 'Polygon' object has no attribute 'contains_points'怎么修改
这个错误是因为 `city_geom` 是一个单个多边形对象,而 `contains_points` 方法是用于点集合的。要解决此错误,可以使用 `contains` 方法来检查 `temp_array` 中的每个点是否在 `city_geom` 中,如下所示:
```
temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])])
```
这将使用列表推导式为 `temp_array` 中的每个点创建一个包含 True 或 False 的数组,并将其传递给 `temp_array`,以便获取在 `city_geom` 中的温度值。
import netCDF4 import geopandas as gpd import numpy as np import pandas as pd # 加载nc文件 nc_file = netCDF4.Dataset('E:/data/temp_CMFD_V0106_B-01_01mo_010deg_197901-201812.nc') temp_data = nc_file.variables['temp'][:] # 加载shp文件 shp_file = gpd.read_file('D:/dilidashuju/shijiquhua.shp') # 计算每个市级行政区的平均温度 years = range(1979, 2018) months = range(1, 13) temp_df = pd.DataFrame(columns=['year', 'month', 'city', 'temperature']) for year in years: for month in months: temp_array = temp_data[(year-1979)*12+month-1, :, :] for i, row in shp_file.iterrows(): city_name = row['市'] city_geom = row['geometry'] temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])]) temp_df = temp_df.append({'year': year, 'month': month, 'city': city_name, 'temperature': temp_mean}, ignore_index=True) # 将结果保存到CSV文件中 temp_df.to_csv('city_temperature.csv', index=False)这串代码报错Traceback (most recent call last): File "C:\Users\Lenovo\PycharmProjects\pythonditu\main.py", line 24, in <module> temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])]) AttributeError: 'MaskedArray' object has no attribute 'lon'怎么修改
这个错误是因为 `temp_array` 是一个 `MaskedArray` 对象,它不能像普通的 numpy 数组那样直接使用索引访问变量。要解决此错误,可以使用 `data` 属性来访问 `temp_array` 的实际数据数组,如下所示:
```
temp_mean = np.mean(temp_array.data[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])])
```
这将使用列表推导式为 `temp_array` 中的每个点创建一个包含 True 或 False 的数组,并将其传递给 `temp_array.data`,以便获取在 `city_geom` 中的温度值。