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 07:32:26 浏览: 112
这个错误是因为 `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.
I'm sorry, but as an AI language model, I don't have access to external files or data sources. However, I can provide you with the code that you can use to accomplish the task described in your question, assuming you have the CSV file with the weather data.
Here is the code:
```python
import pandas as pd
# Read the CSV file into a Pandas DataFrame
df = pd.read_csv('S12_wearther_central_park.csv')
# Format the date column as datetime object and set it as the index
df['DATE'] = pd.to_datetime(df['DATE'])
df.set_index('DATE', inplace=True)
# Get a table with the date as the first column and the daily precipitation,
# maximum temperature, and minimum temperature for x year
x = 2020 # Replace with the desired year
start_date = pd.Timestamp(year=x, month=1, day=1)
end_date = pd.Timestamp(year=x, month=12, day=31)
year_df = df.loc[start_date:end_date, ['PRCP', 'TMIN', 'TMAX']]
year_df.reset_index(inplace=True)
year_df.rename(columns={'DATE': 'Date'}, inplace=True)
# Get a new table from the table when the rainfall greater than 1.3 inches
rainfall_df = year_df[year_df['PRCP'] > 1.3]
# Print the resulting DataFrame
print(rainfall_df)
```
Note that you should replace the `x` variable with the desired year. The code assumes that the CSV file has a header row and that the columns are named `DATE`, `PRCP`, `TMAX`, and `TMIN`. If your CSV file has different column names, you should adjust the code accordingly.
阅读全文
相关推荐














