Aggregate Columns with Part in Common¶
AggregatedColumnAppender
¶
Appends the sum of all columns whose name contains a common part.
Use it for extensive quantities, for example the sum of all wind generation columns. A row where all summed values are NaN gives NaN. With multi-level columns, the sum is per combination of the other levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_common_part
|
str
|
The common substring to search for in column names. All columns containing this substring will be summed together. |
required |
agg_col_name_prefix
|
str
|
Prefix to add to the aggregated column name. Defaults to empty string. |
None
|
agg_col_name_suffix
|
str
|
Suffix to add to the aggregated column name. Defaults to empty string. |
None
|
Examples:
>>> import numpy as np
>>> generation = pd.DataFrame({'wind_onshore': [100.0, np.nan], 'wind_offshore': [50.0, np.nan], 'solar': [30.0, 40.0]})
>>> AggregatedColumnAppender('wind', agg_col_name_suffix='_total').add_aggregated_column(generation)
wind_onshore wind_offshore solar wind_total
0 100.0 50.0 30.0 150.0
1 NaN NaN 40.0 NaN
Source code in src/mesqual/energy_data_handling/variable_utils/aggregate_cols_with_part_in_common.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
add_aggregated_column
¶
add_aggregated_column(df: DataFrame) -> DataFrame
Add an aggregated column to the DataFrame by summing matching columns.
This method identifies all columns containing the specified common part and sums them into a new aggregated column. The aggregation preserves the DataFrame's structure and handles both single-level and multi-level column indices. Without a matching column, single-level columns get a new column of NaN, and multi-level columns get no new column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing energy variables. Can have single-level or multi-level column structure. For multi-level columns, aggregation is performed at the appropriate level while preserving the hierarchy. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Original DataFrame with added aggregated column. The new column name follows the pattern: {prefix}{in_common_part}{suffix} |
Examples:
>>> columns = pd.MultiIndex.from_tuples([('wind_onshore', 'C1'), ('wind_offshore', 'C1'), ('wind_onshore', 'C2')])
>>> generation = pd.DataFrame([[1.0, 2.0, 3.0]], columns=columns)
>>> AggregatedColumnAppender('wind', agg_col_name_prefix='total_').add_aggregated_column(generation)['total_wind']
C1 C2
0 3.0 3.0
Source code in src/mesqual/energy_data_handling/variable_utils/aggregate_cols_with_part_in_common.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |