熊猫数据框,从另一个数据框添加年份和未来年份的计算

时间:2020-02-25 23:36:15

标签: python pandas 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

1 个答案:

答案 0 :(得分:0)

事实证明,我需要进行df.copy才能停止弄乱我的原始数据框,从而使附加操作有效。

null
相关问题