Python Pandas数据帧,从另一个数据帧添加年份和未来年份的计算

Python Pandas数据帧,从另一个数据帧添加年份和未来年份的计算,python,pandas,dataframe,Python,Pandas,Dataframe,这是当前数据帧的一个示例,它是第一天,全部24小时。整个数据帧是一年,分为24小时段 +-------+-----+------+---------------+-------------------+ | month | day | hour | project_name | hourly_production | +-------+-----+------+---------------+-------------------+ | 1 | 1 | 1 | Blah |

这是当前数据帧的一个示例,它是第一天,全部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”对象是可变的,因此它们不能被散列

结果是我需要做一个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)