pandas统计每个月的最高温度和最低温度
时间: 2023-10-14 12:24:22 浏览: 82
假设有一个DataFrame对象df,其中包含日期和温度两列数据,可以使用以下代码统计每个月的最高温度和最低温度:
```python
import pandas as pd
# 读取数据
df = pd.read_csv('data.csv')
# 将日期列转换为日期类型
df['date'] = pd.to_datetime(df['date'])
# 提取月份列
df['month'] = df['date'].dt.month
# 按月份和温度进行分组,统计最高温度和最低温度
result = df.groupby(['month'])['temperature'].agg(['max', 'min'])
print(result)
```
输出结果类似于:
```
max min
month
1 10 -5
2 12 -3
3 18 2
4 25 9
5 30 15
6 35 20
7 40 25
8 38 23
9 32 18
10 25 10
11 18 2
12 12 -3
```
其中,每行表示一个月份的最高温度和最低温度。
相关问题
python怎么利用循环计算每个月的平均最高温度和最低温度
如果你有一个包含每天温度的数据帧,并且每个月的数据都在单独的行中,那么你可以使用pandas和循环来计算每个月的平均最高温度和最低温度。以下是一些示例代码:
```python
import pandas as pd
# 生成示例数据
data = {'日期': pd.date_range(start='2021-01-01', end='2021-12-31'),
'温度': [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6,
8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6,
10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6,
10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6,
10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6,
10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 25, 20, 15, 10, 8, 6, 4, 2, 0, -2, -4, -5, -6]}
df = pd.DataFrame(data)
# 添加月份列
df['月份'] = df['日期'].dt.month
# 循环计算每个月的平均最高温度和最低温度
for month in range(1, 13):
month_data = df[df['月份'] == month]
avg_high_temp = month_data['温度'].max()
avg_low_temp = month_data['温度'].min()
print(f"{month}月的平均最高温度为:{avg_high_temp},平均最低温度为:{avg_low_temp}")
```
此代码将生成一个具有365天数据的数据帧,并将每个月的数据分组到单独的行中。然后,使用循环计算每个月的平均最高温度和最低温度,并将其打印出来。
pandas 取每个月最后一天
可以使用 pandas 中的 resample 方法来实现取每个月最后一天的操作,具体代码如下:
```python
import pandas as pd
# 创建一个时间序列
dates = pd.date_range('2020-01-01', '2021-12-31', freq='D')
# 创建一个 DataFrame
df = pd.DataFrame({'date': dates, 'value': range(len(dates))})
# 将 date 列设置为索引列
df.set_index('date', inplace=True)
# 使用 resample 方法取每个月最后一天
result = df.resample('M').last()
print(result.head())
```
输出结果为:
```
value
date
2020-01-31 30
2020-02-29 59
2020-03-31 90
2020-04-30 120
2020-05-31 151
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)