pythonpandasdataframequartile

Dataframe - quartile from rows


df:

a | b | c | d | q3 - q1 from (a,b,c,d) 
1 | 2 | 3 | 4 | result
5 | 6 | 2 | 7 | result

Can someone help me with this result?

(q -> quartile)


Solution

  • The quartile is the 25 % quantile, so you can use the quantile function along the columns axis:

    import pandas as pd
    df = pd.DataFrame({'a': [1,5], 'b': [2,6], 'c': [3,2], 'd': [4,7]})
    df['q3-q1'] = df.quantile(.75, axis=1) - df.quantile(.25, axis=1)
    

    Result:

       a  b  c  d  q3-q1
    0  1  2  3  4    1.5
    1  5  6  2  7    2.0