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.
时间: 2024-01-30 14:04:31 浏览: 66
对于您的问题,我可以给出以下Python代码实现:
```python
import pandas as pd
# 读取数据
df = pd.read_csv('S12_wearther_central_park.csv')
# 将Date列转换为datetime类型并设置为索引
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# 获取指定年份的数据
def get_year_data(year):
start_date = pd.to_datetime(str(year) + '-01-01')
end_date = pd.to_datetime(str(year) + '-12-31')
year_data = df.loc[start_date:end_date, ['PRCP', 'TMIN', 'TMAX']]
return year_data
# 获取降雨量大于1.3英寸的数据
rainfall_data = df[df['PRCP'] > 1.3]
# 获取指定年份降雨量大于1.3英寸的数据
def get_year_rainfall_data(year):
year_data = get_year_data(year)
year_rainfall_data = rainfall_data.loc[year_data.index]
return year_rainfall_data
# 测试代码
year = 2010
year_rainfall_data = get_year_rainfall_data(year)
print(year_rainfall_data)
```
您只需将代码中的文件路径修改为您的数据文件路径,并将输入年份传递给`get_year_rainfall_data()`函数即可得到所需的结果。
阅读全文