Код: Выделить всё
| date | group_id | inner_id | attr_a | attr_b | attr_c |
|------------|----------|----------|--------|--------|--------|
| 2023-06-01 | A1 | 001 | val | val | val |
| 2023-06-01 | A1 | 002 | val | val | val |
...
Код: Выделить всё
| date | matrix |
|------------|--------------|
| 2023-06-01 | [[...], ...] |
...
Код: Выделить всё
def run(group_data: pd.DataFrame, matrix) -> pd.DataFrame:
# process data
return processed_data
Код: Выделить всё
| inner_id | attr_a | attr_b | attr_c |
|----------|--------|--------|--------|
| 001 | val | val | val |
...
Код: Выделить всё
def calculate_metrics(data: DataFrame, matrices: DataFrame) -> DataFrame:
# Convert matrices to a dictionary mapping dates to matrix
date_matrices = matrices.rdd.collectAsMap()
# Broadcast the matrices
broadcasted_matrices = spark_context.broadcast(date_matrices)
# Function to apply calculations
def apply_calculation(group_key: Tuple[str, str], data_group: pd.DataFrame) -> pd.DataFrame:
date = group_key[1]
return custom_calculation_function(broadcasted_matrices.value[date], data_group)
# Apply the function to each group
return data.groupby('group_id', 'date').applyInPandas(apply_calculation, schema_of_result)
Подробнее здесь: https://stackoverflow.com/questions/786 ... on-one-key