[英]Pandas Dataframe, adding years and calculations for future years from another dataframe
这是当前数据帧的一个示例,它是第一天和所有 24 小时。 整个数据框是一年,分解为 24 小时段
+-------+-----+------+---------------+-------------------+
| month | day | hour | project_name | hourly_production |
+-------+-----+------+---------------+-------------------+
| 1 | 1 | 1 | Blah | 0 |
| 1 | 1 | 2 | Blah | 0 |
| 1 | 1 | 3 | Blah | 0 |
| 1 | 1 | 4 | Blah | 0 |
| 1 | 1 | 5 | Blah | 0 |
| 1 | 1 | 6 | Blah | 0 |
| 1 | 1 | 7 | Blah | 0 |
| 1 | 1 | 8 | Blah | 1.44 |
| 1 | 1 | 9 | Blah | 40.42 |
| 1 | 1 | 10 | Blah | 49.13 |
| 1 | 1 | 11 | Blah | 47.57 |
| 1 | 1 | 12 | Blah | 43.77 |
| 1 | 1 | 13 | Blah | 42.33 |
| 1 | 1 | 14 | Blah | 45.25 |
| 1 | 1 | 15 | Blah | 48.54 |
| 1 | 1 | 16 | Blah | 46.34 |
| 1 | 1 | 17 | Blah | 18.35 |
| 1 | 1 | 18 | Blah | 0 |
| 1 | 1 | 19 | Blah | 0 |
| 1 | 1 | 20 | Blah | 0 |
| 1 | 1 | 21 | Blah | 0 |
| 1 | 1 | 22 | Blah | 0 |
| 1 | 1 | 23 | Blah | 0 |
| 1 | 1 | 24 | Blah | 0 |
+-------+-----+------+---------------+-------------------+
这是我当前的代码:
df0_partition_1 = df0[['project_id', 'start_date', 'degradation_factor', 'snapshot_datetime']]
df0_partition_2 = df0_partition_1.groupby(['project_id', 'start_date', 'degradation_factor_solar', 'snapshot_datetime']).size().reset_index()
df2_partition_1 = df2[df2['duration_year']==df2['duration_year'].max()]
df2_partition_2 = df2_partition_1.groupby(['project_id', 'snapshot_datetime']).size().reset_index()
df_merge = pd.merge(df0_partition_2, df2_partition_2, on=['project_id', 'snapshot_datetime'], how='left')
df_merge.rename(columns={'0_y':'duration_year'}, inplace=True)
df_parts = df_merge[['project_id', 'start_date', 'duration_year', 'degradation_factor_solar', 'snapshot_datetime']].dropna()
for index, row in df_parts.iterrows():
df1_filtered = df1[(df1['project_id'] == row['project_id']) &
(df1['snapshot_datetime'] == row['snapshot_datetime'])]
df1_filtered['year'] = pd.to_datetime(row['start_date']).year
for y in range(1, int(row['duration_year'])+1):
df_stg = df_stg = df1_filtered[[df1_filtered['year'] + y, df1_filtered['hourly_production']*(1-(float(row.loc['degradation_factor_solar'].strip('%'))*y/100))]]
df_final = df1_filtered.append(df_stg)
我需要帮助弄清楚如何创建最终的数据框。 最终数据框是未来年份的附加,其中降级因子应用于每小时生产。 我不确定如何在 DF 中增加年份并应用退化因子然后附加。
现在这给了我TypeError: 'Series' objects are mutable, thus they cannot be hashed
原来我需要做一个 df.copy 以停止弄乱我的原始数据框,从而有一个有效的附加。
df0_partition_1 = df0[['project_id', 'start_date', 'degradation_factor_solar', 'snapshot_datetime']]
df0_partition_2 = df0_partition_1.groupby(['project_id', 'start_date', 'degradation_factor', 'snapshot_datetime']).size().reset_index()
df2_partition_1 = df2[df2['duration_year']==df2['duration_year'].max()]
df2_partition_2 = df2_partition_1.groupby(['project_id', 'snapshot_datetime']).size().reset_index()
df_merge = pd.merge(df0_partition_2, df2_partition_2, on=['project_id', 'snapshot_datetime'], how='left')
df_merge.rename(columns={'0_y':'duration_year'}, inplace=True)
df_parts = df_merge[['project_id', 'start_date', 'duration_year', 'degradation_factor', 'snapshot_datetime']].dropna()
for index, row in df_parts.iterrows():
df1_filtered = df1[(df1['project_id'] == row['project_id']) &
(df1['snapshot_datetime'] == row['snapshot_datetime'])]
df1_filtered['year'] = pd.to_datetime(row['start_date']).year
df1_filtered.reset_index(inplace=True, drop=True)
df1_filtered.drop(columns='project_name', inplace=True)
df_stg_1 = df1_filtered.copy()
for y in range(2, int(row['duration_year'])+1):
year = df1_filtered['year']+(y-1)
hourly_production = df1_filtered['hourly_production']
df_stg_1['year'] = year
df_stg_1['hourly_production'] = hourly_production*(1-(float(row.loc['degradation_factor_solar'].strip('%'))*(y-1)/100))
df_stg_2 = df1_filtered.append(df_stg_1)
df_final = df1_filtered.append(df_stg_2)
df_final.reset_index(inplace=True, drop=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.