pythonpandasdeltabroadcasting

Find minimum delta between two cells for each column in pandas


Given a dataframe (e.g.)

df = pd.DataFrame({"A": [9, 6, 9, 4, 5], "B": [8, 2, 9, 7, 3], "C": [0, 9, 3, 15, 5],})

I'd like to return a dataframe with columns A, B, C and a single row, where the row value is the smallest absolute delta between the cells in A, B, C (e.g.)

df_result = pd.DataFrame(
    {
        "A": [0], # 9-9
        "B": [1], # 2-3, 9-8, 8-7
        "C": [2], # 5-3
    }
)

I'd like to do this generally. That is take in a dataframe then find the delta between all the cells except itself, and find the minimum value. Then that over each column. Then return a dataframe with the same columns and one row which contains the absolute value of the mininmum delta.


Solution

  • You can sort_values then compute the diff and finally get the min value:

    >>> df.agg(lambda x: x.sort_values().diff().min())
    A    0.0
    B    1.0
    C    2.0
    dtype: float64
    

    Example for A:

    >>> df['A'].sort_values()
    3    4
    4    5
    1    6
    0    9
    2    9
    Name: A, dtype: int64
    
    >>> df['A'].sort_values().diff()
    3    NaN
    4    1.0
    1    1.0
    0    3.0
    2    0.0
    Name: A, dtype: float64
    
    >>> df['A'].sort_values().diff().min()
    0.0