Pandas Util combine_dfs¶
combine_dfs
¶
combine_dfs(dfs: Iterable[Series | DataFrame], precedence: Literal['first', 'last'] = 'first') -> Series | DataFrame
Combine multiple DataFrames or Series into one.
The index and column structure of the inputs selects the method:
- Shared index, no shared columns: concatenate along the columns.
- Shared columns, no shared index: concatenate along the rows.
- Otherwise:
combine_first, so that one input fills the gaps of the other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dfs
|
Iterable[Series | DataFrame]
|
An iterable of pandas DataFrames or Series to combine. |
required |
precedence
|
Literal['first', 'last']
|
Which input wins where two inputs overlap in index and
columns: |
'first'
|
Returns:
| Type | Description |
|---|---|
Series | DataFrame
|
A single DataFrame or Series containing the combined data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no DataFrames are provided in the iterable, or
|
Use it to join results that come in parts, for example one run per month, or a yearly run in which one week is replaced by a rerun.
Examples:
>>> first = pd.DataFrame({'A': [1, 2]}, index=['x', 'y'])
>>> combine_dfs([first, pd.DataFrame({'B': [3, 4]}, index=['x', 'y'])]) # new columns
A B
x 1 3
y 2 4
>>> combine_dfs([first, pd.DataFrame({'A': [5]}, index=['z'])]) # new rows
A
x 1
y 2
z 5
Source code in src/mesqual/utils/pandas_utils/combine_df.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 | |