Elysire Elysire - 7 days ago 7
Python Question

Pandas Median on index not values

My datas :

LogRatio Strength
0.555 9.1
0.542 9.6
0.533 9.7
0.532 9.3
0.519 9.2
0.508 9.5


I want to have the point(LogRatio,Strength) that is the median position of my group indexA-indexB

indexA = 0
indexB = 4
point_logRatio = df['LogRatio'][indexA:indexB].median()
point_Strength = df[df['LogRatio']==point_logRatio['Strength'].iloc[0]


But I don't want to calcul the median with values, I just want the index of the row to get the LogRatio and the strength corresponding. Because point_logRatio calcul the median and don't give me just the row.
That's why my point_Strength doesn't work because the median calculated don't match with a value of the dataframe.

So, if you have an easier way to get the LogRatio and the Strength at the same time where LogRatio is at a median position in my dataframe between indexA and indexB, thank you.

At the end, I would like to have :
point = (0.533 , 9.7)

Because 0.533 is the logRatio at the median position between my index 0 and 4 in my dataframe and the strength corresponding is 9.7.

Answer

you can try

median_position = (indexA+indexB)/2
point_logRatio  = df.iloc[median_position]['LogRatio']
point_Strength  = df.iloc[median_position]['Strength']