Get corresponding index of median

Viewed 6491

I have a pandas dataframe with one column and I would like to know the index of the median. That is, I determine the median this way:

df.median()

This gives me the median value, but I would like to know the index of that row. Is it possible to determine this? For a list with uneven length I could search for the index with that value but for even list lengths this is not going to work. Can someone help?

This question was asked in another post, where the answer was basically to search for rows which have the same value as the median. But like I said, that will not work for a list of even length.

Below is a Min Example (I have included the suggestion by Wen below):

df = pd.DataFrame(np.random.randn(6, 1), columns=list('A'))
df.median()
df.loc[df[0]==df[0].median()]

Out[120]: 
Empty DataFrame
Columns: [0]
Index: []
2 Answers

You can use Wen's answer for dataframes of odd length.

For dataframes of even length, the question does not really make sense. As you have pointed out the median does not exist in the dataframe. However, you can sort the dataframe by your column of interest and then find the indices for the two "median" values.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(6, 1), columns=list('A'))
df.median()

df.loc[df['A']==df['A'].median()]

df.sort_values(by='A', inplace=True)

df[df['A'] > df['A'].median()].iloc[0]
df[df['A'] < df['A'].median()].iloc[-1]

Another way is to use the quantile function (which convieniently defaults to 0.5, i.e. the median) and set the interpolation argument so that it doesn't try to split the midpoints on a DataFrame of even length.

import pandas as pd
import numpy as np

df=pd.DataFrame(np.random.randn(6,1), columns=['A'])


# row nearest to midpoint 
df[df['A']==df['A'].quantile(interpolation='nearest')]

# just below the midpoint
df[df['A']==df['A'].quantile(interpolation='lower')]

# just above the midpoint
df[df['A']==df['A'].quantile(interpolation='higher')]
Related