pynetdesign.modelling.io.combine_geometry#

pynetdesign.modelling.io.combine_geometry(*dataframes, names=None)[source]#

Combine an arbitrary number of pandas DataFrames with geometries into one while retaining the ability to distinguish and recover the original DataFrames.

This function adds a ‘source’ column to each DataFrame to identify its origin, concatenates them, and sets a multi-level index using the ‘source’ column and the original index.

It also stores and recovers attributes of the original dataframes.

Parameters:
*dataframespandas.DataFrame

An arbitrary number of DataFrames to be combined.

namestyping.Union[List[str], Dict[int, str]], optional

Names to use as identifiers for the DataFrames. Can be a list of strings or a dictionary mapping DataFrame positions to names. If not provided, default names ‘df0’, ‘df1’, etc. will be used.

Returns:
combined: pandas.DataFrame

A combined DataFrame with a multi-level index and additional methods:

  • get_names(): Returns a list of internal DataFrame names;

  • recover(name): Recovers a specific DataFrame by name.

Raises:
ValueError

If no DataFrames are provided, or if the number of names doesn’t match the number of DataFrames.

Examples

>>> import pandas as pd
>>> df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
>>> df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
>>> df3 = pd.DataFrame({'A': [9, 10], 'B': [11, 12]})
>>> combined = combine_df(df1, df2, df3, names=['First', 'Second', 'Third'])
>>> print(combined.get_names())
['First', 'Second', 'Third']
>>> recovered_df2 = combined.recover('Second')
>>> print(recovered_df2)
   A  B
0  5  7
1  6  8