Search code examples
pythonpandasdataframeindexingmedian

Get corresponding index of median


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: []

Solution

  • 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]