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'怎么修改
时间: 2024-04-03 08:32:26 浏览: 90
这个错误是因为 `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` 中的温度值。
Weather Read the weather data for Central Park in New York City between Jan 1, 2000 and Dec 31, 2020 in the file S12_wearther_central_park.csv into a Pandas DataFrame. Format the date as datetime object and set to be the DataFrame index. Get a table with the date as the first column and the daily precipitation, maximum temperature, and minimum temperature for x year. The headings for those data are PRCP, TMIN, and TMAX, respectively. Finally, get a new table from the table when the rainfall greater than 1.3 inches and print out. Input An integer x. Output A DataFram containing data for rainfall of more than 1.3 inches in x year.
Sure, here's how you can do it in Python using Pandas:
```python
import pandas as pd
# Read the CSV file into a DataFrame
df = pd.read_csv('S12_wearther_central_park.csv')
# Convert the date column to datetime and set as index
df['DATE'] = pd.to_datetime(df['DATE'])
df.set_index('DATE', inplace=True)
# Create a new DataFrame with the desired columns for x year
x = 2010
year_df = df.loc[str(x), ['PRCP', 'TMIN', 'TMAX']]
# Filter the DataFrame for rainfall greater than 1.3 inches
rainy_days = year_df[year_df['PRCP'] > 1.3]
# Print the resulting DataFrame
print(rainy_days)
```
This code reads the weather data from the CSV file into a DataFrame, converts the date column to datetime and sets it as the index. It then creates a new DataFrame with the precipitation, minimum temperature, and maximum temperature for the specified year (in this case, 2010). Finally, it filters the DataFrame to only include days with rainfall greater than 1.3 inches and prints the resulting DataFrame. You can change the value of `x` to get data for a different year.
阅读全文