mspu.pandas.df_diffs#
- mspu.pandas.df_diffs(df1: DataFrame, df2: DataFrame, left_suffix: str = '_df1', right_suffix: str = '_df2') DataFrame[source]#
Get rows in two dfs that are different to each other.
Comparison is based on df MultiIndex.
- Parameters:
- df1pd.DataFrame
First DataFrame to compare.
- df2pd.DataFrame
Second DataFrame to compare.
- left_suffixstr
Suffix to append to column names from df1 in the output DataFrame.
- right_suffixstr
Suffix to append to column names from df2 in the output DataFrame.
- Returns:
- pd.DataFrame
DataFrame containing rows that are different between df1 and df2, with suffixes indicating the source of each column.
Examples
>>> import pandas as pd >>> from mspu.pandas import df_diffs >>> df1 = pd.DataFrame({ ... 'fruit': ['apple', 'banana', 'apple'], ... 'id': [1, 2, 4], ... 'store': ['us', 'uk', 'cn'], ... 'price': [4, 3.1, 2.5], ... }).set_index(['fruit', 'id']) >>> print(df1) store price fruit id apple 1 us 4.0 banana 2 uk 3.1 apple 4 cn 2.5 >>> df2 = pd.DataFrame({ ... 'fruit': ['apple', 'banana', 'apple'], ... 'id': [1, 3, 4], ... 'store': ['us', 'uk', 'cn'], ... 'price': [4, 3.2, 2.5], ... }).set_index(['fruit', 'id']) >>> print(df2) store price fruit id apple 1 us 4.0 banana 3 uk 3.2 apple 4 cn 2.5 >>> diff = df_diffs(df1, df2) >>> print(diff) store_df1 price_df1 store_df2 price_df2 fruit id banana 2 uk 3.1 NaN NaN 3 NaN NaN uk 3.2