how to get a index of row after it satisfies certain condition

a data frame of the country name in rows with corresponding medals win in summer and winter Olympics

I want in this data frame to get the country name which has a max difference in summer gold and winter gold, let’s say summer gold column name is x and winter gold column name is y

all the country names are an index of rows

Add Comment
1 Answer(s)

It is always good to provide a sample data frame so we can help better. I think you are looking for this:

(df.y-df.x).idxmax() 

And if you care only about the absolute value of difference:

(df.x-df.y).abs().idxmax() 

Example:

df = pd.DataFrame({'x':[1,2,3],'y':[2,10,5]}, index=['a','b','c'])     x   y a  1   2 b  2  10 c  3   5  print((df.y-df.x).abs().idxmax())  b 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.