meld_along_columns
- trashpanda.meld_along_columns(left: Union[pandas.core.series.Series, pandas.core.frame.DataFrame], right: Union[pandas.core.series.Series, pandas.core.frame.DataFrame], copy_at_meld: bool = True, keep: Union[str, bool] = 'raise') pandas.core.frame.DataFrame
Melds two DataFrames of Series into a single DataFrame with a common index.
Notes
This method is called meld because it doesn’t fit into existing categories of pandas join, merge or concat.
Warning
This method will override values of equal named columns in left with right.
- Parameters
left (DataFrame) – Left curve to be merged with the right one.
right (DataFrame) – Right curve, which will merge to the left one.
copy_at_meld (bool) – Makes a copy during the concat process, creating a new DataFrame instead of overriding the source.
keep (Union[str, bool]) – Determines which duplicates to keep. - first: Default; keeps all first occurrences of duplicated indexes. - last: Keeps all last occurrences of duplicated indexes. - False: Drops all duplicated indexes.
- Returns
DataFrame
Examples
Melding of two DataFrame.
>>> from pathlib import Path >>> from pandas import DataFrame, Series >>> import numpy as np >>> import examplecurves >>> left_curve, right_curve = examplecurves.Static.create( ... family_name="nonlinear0", cut_curves_at=3, curve_selection=[1, 2] ... ) >>> left_curve.columns = ["left"] >>> right_curve.columns = ["right"] >>> from doctestprinter import doctest_print >>> doctest_print(meld_along_columns(left_curve, right_curve)) left right x 0.000 0.0000 0.000000 0.100 1.5625 NaN 0.111 NaN 1.607654 0.200 3.0000 NaN 0.222 NaN 3.085479
Melding of 2 Series.
>>> left_series = left_curve["left"] >>> right_series = right_curve["right"] >>> doctest_print(meld_along_columns(left_curve, right_curve)) left right x 0.000 0.0000 0.000000 0.100 1.5625 NaN 0.111 NaN 1.607654 0.200 3.0000 NaN 0.222 NaN 3.085479
Example of melding two DataFrames with duplicated indexes and one duplicated column. Left values of the intersecting columns are being overriden with the right values.
>>> left_sample = pandas.DataFrame( ... np.arange(10).reshape(5, 2), ... columns=["b", "a"], ... index=pandas.Index(np.linspace(0.1, 0.5, num=5), name="x") ... ) >>> doctest_print(left_sample) b a x 0.1 0 1 0.2 2 3 0.3 4 5 0.4 6 7 0.5 8 9 >>> right_sample = pandas.DataFrame( ... np.linspace(0.5, 9.5, num=10).reshape(5, 2), ... columns=["a", "c"], ... index=pandas.Index([0.1, 0.1, 0.15, 0.15, 0.2], name="x") ... ) >>> doctest_print(right_sample) a c x 0.10 0.5 1.5 0.10 2.5 3.5 0.15 4.5 5.5 0.15 6.5 7.5 0.20 8.5 9.5 >>> merged_frame = meld_along_columns(left_sample, right_sample, keep='first') >>> doctest_print(merged_frame) b a c x 0.10 0.0 0.5 1.5 0.15 NaN 4.5 5.5 0.20 2.0 8.5 9.5 0.30 4.0 5.0 NaN 0.40 6.0 7.0 NaN 0.50 8.0 9.0 NaN