Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding minimum value for each level of a multi-index dataframe

Tags:

python

pandas

I have a DataFrame that looks like this:

        data
a   b
1   1   0.1
    2   0.2
    3   0.3
2   1   0.5
    2   0.6
    3   0.7

and I want to find the minimum value for each level of a ignoring the b level, so as an output I'm looking for something like

a   min
1   0.1
2   0.5
like image 499
David Stansby Avatar asked Jan 11 '17 11:01

David Stansby


People also ask

How do you find the minimum value of a data frame?

To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument axis=1 i.e. It returned a series with row index label and minimum value of each row.

How do you find the minimum value of a series?

min() function to find the minimum value among the underlying data in the given series object. Output : Now we will use Series. min() function to find the minimum value of the given series object.


1 Answers

The simpliest is use min with parameter level=0:

print (df.data.min(level=0).reset_index(name='min'))
   a  min
0  1  0.1
1  2  0.5

If need output as df and only one column df:

print (df.min(level=0))
   data
a      
1   0.1
2   0.5

Or groupby by first level with aggregating min:

print (df.groupby(level=0).data.min().reset_index(name='min'))
   a  min
0  1  0.1
1  2  0.5
like image 167
jezrael Avatar answered Nov 07 '22 01:11

jezrael