Give this dataframe ‘x’:
col1 col2 col3 col4 0 5 -2 1 -5 2 -1 9 3 -7 3 5
How I could get a list of pairs with the min and max of each column? The result would be:
list = [ [-5 , 3], [-7 , 5], [-2 , 3], [1 , 9] ]
Advertisement
Answer
You could define a function and call apply
passing the function name, this will create a df with min and max as the index names:
In [203]: def minMax(x): return pd.Series(index=['min','max'],data=[x.min(),x.max()]) df.apply(minMax) Out[203]: col1 col2 col3 col4 min -5 -7 -2 1 max 3 5 3 9
If you insist on a list of lists we can transpose the df and convert the values to a list:
In [206]: def minMax(x): return pd.Series(index=['min','max'],data=[x.min(),x.max()]) df.apply(minMax).T.values.tolist() Out[206]: [[-5, 3], [-7, 5], [-2, 3], [1, 9]]
The function itself is not entirely necessary as you can use a lambda instead:
In [209]: df.apply(lambda x: pd.Series([x.min(), x.max()])).T.values.tolist() Out[209]: [[-5, 3], [-7, 5], [-2, 3], [1, 9]]
Note also that you can use describe
and loc
to get what you want:
In [212]: df.describe().loc[['min','max']] Out[212]: col1 col2 col3 col4 min -5 -7 -2 1 max 3 5 3 9