Search code examples
pandasaggregationminimum

Find the minimum value in each column with their index


I wonder if there is any method that I can use to get the minimum value in each column with its index id without using the aggregate method (df.agg(['min','idmin']).T) for this code?!!

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

Solution

  • Construct a DataFrame from scratch:

    pd.DataFrame({'min': df.min(), 'idxmin': df.idxmin()})
    
           min     idxmin
    A -1.106929 2013-01-03
    B -0.642583 2013-01-06
    C -1.350638 2013-01-04
    D -0.213750 2013-01-06
    

    or concat the two Series:

    pd.concat((df.min(), df.idxmin()), axis=1).rename(columns={0: 'min', 1: 'idxmin'})
    Out: 
            min     idxmin
    A -1.106929 2013-01-03
    B -0.642583 2013-01-06
    C -1.350638 2013-01-04
    D -0.213750 2013-01-06