如何使用 pandas 创建月度和季度 24 小时平均值 table
how to create monthly and season 24 hours average table using pandas
我有一个包含 2 列的数据框:Date
和 LMP
,总共有 8760 行。这是虚拟数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date': pd.date_range('2023-01-01 00:00', '2023-12-31 23:00', freq='1H'), 'LMP': np.random.randint(10, 20, 8760)})
我从日期中提取 month
,然后为特定日期创建了 season
列。像这样
df['month'] = pd.DatetimeIndex(df['Date']).month
season = []
for i in df['month']:
if i <= 2 or i == 12:
season.append('Winter')
elif 2 < i <= 5:
season.append('Spring')
elif 5 < i <= 8:
season.append('Summer')
else:
season.append('Autumn')
df['Season'] = season
df2 = df.groupby(['month']).mean()
df3 = df.groupby(['Season']).mean()
print(df2['LMP'])
print(df3['LMP'])
输出:
**month**
1 20.655113
2 20.885532
3 19.416946
4 22.025248
5 26.040606
6 19.323863
7 51.117965
8 51.434093
9 21.404680
10 14.701989
11 20.009590
12 38.706160
**Season**
Autumn 18.661426
Spring 22.499365
Summer 40.856845
Winter 26.944382
但我希望 monthly
和 seasonal
的输出都为 24 小时平均值。
期望的输出:
季节性 24 小时平均值
对于monthyl 24 小时平均值
注:monthyl 24小时平均列为月(1,2,3,4,5,6,7,8,9,10,11,12),行为小时(从0开始) .
有人可以帮忙吗?
尝试:
df['hour']=pd.DatetimeIndex(df['Date']).hour
dft = df[['Season', 'hour', 'LMP']]
dftg = dft.groupby(['hour', 'Season'])['LMP'].mean()
dftg.reset_index().pivot(index='hour', columns='Season')
结果:
我有一个包含 2 列的数据框:Date
和 LMP
,总共有 8760 行。这是虚拟数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Date': pd.date_range('2023-01-01 00:00', '2023-12-31 23:00', freq='1H'), 'LMP': np.random.randint(10, 20, 8760)})
我从日期中提取 month
,然后为特定日期创建了 season
列。像这样
df['month'] = pd.DatetimeIndex(df['Date']).month
season = []
for i in df['month']:
if i <= 2 or i == 12:
season.append('Winter')
elif 2 < i <= 5:
season.append('Spring')
elif 5 < i <= 8:
season.append('Summer')
else:
season.append('Autumn')
df['Season'] = season
df2 = df.groupby(['month']).mean()
df3 = df.groupby(['Season']).mean()
print(df2['LMP'])
print(df3['LMP'])
输出:
**month**
1 20.655113
2 20.885532
3 19.416946
4 22.025248
5 26.040606
6 19.323863
7 51.117965
8 51.434093
9 21.404680
10 14.701989
11 20.009590
12 38.706160
**Season**
Autumn 18.661426
Spring 22.499365
Summer 40.856845
Winter 26.944382
但我希望 monthly
和 seasonal
的输出都为 24 小时平均值。
期望的输出:
季节性 24 小时平均值
对于monthyl 24 小时平均值
注:monthyl 24小时平均列为月(1,2,3,4,5,6,7,8,9,10,11,12),行为小时(从0开始) . 有人可以帮忙吗?
尝试:
df['hour']=pd.DatetimeIndex(df['Date']).hour
dft = df[['Season', 'hour', 'LMP']]
dftg = dft.groupby(['hour', 'Season'])['LMP'].mean()
dftg.reset_index().pivot(index='hour', columns='Season')
结果: