geost.accessor.GeostFrame.aggregate_consecutive_layers#

GeostFrame.aggregate_consecutive_layers(over: str | list[str], agg_funcs: dict = None, keep_original_index: bool = False) → DataFrame[source]#

Aggregate consecutive layers in a DataFrame over columns that have the same value or values for consecutive rows. The column to use for aggregating layers is typically categorical data, such as lithology or soil type. Any other columns can be aggregated using the provided aggregation functions provided by agg_funcs.

Parameters:
  • over (str | list[str]) – Name or names of the column or columns to aggregate over (i.e., to check for consecutive identical values). Typically columns holding categorical data.

  • agg_funcs (dict, optional) – Dictionary specifying the aggregation functions to apply to other columns when combining layers. Keys are column names, and values are aggregation functions. These can be e.g. names such as’first’, ‘last’, ‘mean’, etc. or actual functions such as np.sum, np.mean, etc.

  • keep_original_index (bool, optional) – If True, the original index of the DataFrame is preserved in the resulting DataFrame. If False, the index is reset (recommended). The default is False.

Returns:

New DataFrame with consecutive layers combined based on the specified column.

Return type:

pd.DataFrame

Raises:

KeyError – If the specified column does not exist in the DataFrame.

Notes

When you perform this function on discrete data such as CPT’s, the result of the aggregation is layered data defined by top and bottom depths.

Examples

Say we have borehole data and we want to combine consecutive layers with the same lithology.

>>> import geost
>>> import pandas as pd
>>> boreholes = pd.DataFrame(
...     {
...         "nr": ["A", "A", "A", "B", "B", "B"],
...         "surface": [0.2, 0.2, 0.2, 0.3, 0.3, 0.3],
...         "top": [0, 0.8, 1.2, 0, 1.0, 1.5],
...         "bottom": [0.8, 1.2, 2.0, 1.0, 1.5, 2.0],
...         "lith": ["K", "K", "Z", "K", "K", "K"],
...         "value": [10, 20, 30, 40, 50, 60],
...     }
... )
>>> boreholes
  nr  surface  top  bottom lith  value
0  A      0.2  0.0     0.8    K     10
1  A      0.2  0.8     1.2    K     20
2  A      0.2  1.2     2.0    Z     30
3  B      0.3  0.0     1.0    K     40
4  B      0.3  1.0     1.5    K     50
5  B      0.3  1.5     2.0    K     60
>>> result = boreholes.gst.aggregate_consecutive_layers('lith')
>>> result
  nr  surface  top  bottom lith  value
0  A      0.2  0.0     1.2    K     10
1  A      0.2  1.2     2.0    Z     30
2  B      0.3  0.0     2.0    K     40

We can also use a custom aggregation function by providing the agg_funcs parameter. For example, to take the mean of the ‘value’ column for consecutive layers with the same lithology:

>>> result = boreholes.gst.aggregate_consecutive_layers('lith', agg_funcs={'value': 'mean'})
>>> result
  nr  surface  top  bottom lith  value
0  A      0.2  0.0     1.2    K     15
1  A      0.2  1.2     2.0    Z     30
2  B      0.3  0.0     2.0    K     50