Skip to content

8 Stitching function

View or edit on GitHub

This page is synchronized from trase/models/lp/8_Stitching_function.ipynb. Last modified on 2026-05-06 16:54 CEST by Trase Admin. Please view or edit the original file there; changes should be reflected here after a midnight build (CET time), or manually triggering it with a GitHub action (link).

Stitching function

import pandas as pd

from trase.tools.sps import stitch_dataframes


df_1 = pd.DataFrame(
    data=[
        ("FARM-1", "SILO-1", 55),
        ("FARM-2", "SILO-1", 55),
        ("FARM-1", "SILO-2", 10),
        ("FARM-3", "SILO-3", 20),
    ],
    columns=["farm", "silo", "vol"],
)
df_1
farm silo vol
0 FARM-1 SILO-1 55
1 FARM-2 SILO-1 55
2 FARM-1 SILO-2 10
3 FARM-3 SILO-3 20
df_2 = pd.DataFrame(
    data=[
        ("SILO-1", "PORT-1", 50),
        ("SILO-1", "PORT-2", 50),
        ("SILO-2", "PORT-1", 50),
    ],
    columns=["silo", "port", "vol"],
)
df_2
silo port vol
0 SILO-1 PORT-1 50
1 SILO-1 PORT-2 50
2 SILO-2 PORT-1 50
df = stitch_dataframes(df_1, df_2, volume_column="vol")
df
farm matched port silo vol
0 FARM-1 True PORT-1 SILO-1 25.0
1 FARM-2 True PORT-1 SILO-1 25.0
2 FARM-1 True PORT-2 SILO-1 25.0
3 FARM-2 True PORT-2 SILO-1 25.0
4 FARM-1 True PORT-1 SILO-2 10.0
0 FARM-1 False UNKNOWN SILO-1 5.0
4 FARM-2 False UNKNOWN SILO-1 5.0
6 FARM-3 False UNKNOWN SILO-3 20.0
4 UNKNOWN False PORT-1 SILO-2 40.0
df = stitch_dataframes(df_1, df_2, volume_column="vol", indicator=True)
df
_matched farm port silo vol
0 both FARM-1 PORT-1 SILO-1 25.0
1 both FARM-2 PORT-1 SILO-1 25.0
2 both FARM-1 PORT-2 SILO-1 25.0
3 both FARM-2 PORT-2 SILO-1 25.0
4 both FARM-1 PORT-1 SILO-2 10.0
0 left_only FARM-1 UNKNOWN SILO-1 5.0
4 left_only FARM-2 UNKNOWN SILO-1 5.0
6 left_only FARM-3 UNKNOWN SILO-3 20.0
4 right_only UNKNOWN PORT-1 SILO-2 40.0