Zhubarb
Zhubarb

Reputation: 11895

Pandas rank subset of rows based on condition column

I want to rank the below dataframe by score, only for rows wherecondition is False. The rest should have a rank of NaN.

df=pd.DataFrame(np.array([[34, 65, 12, 98, 5],[False, False, True, False, False]]).T, index=['A', 'B','C','D','E'], columns=['score', 'condition'])

The desired output with the (descending) conditional rank would be:

   score  condition  cond_rank
A     34          0     3 
B     65          0     2
C     12          1    NaN
D     98          0     1
E      5          0     4

I know pd.DataFrame.rank() can handle NaN for the values that are being ranked, but in cases where the conditioning is intended on another column/series, what is the most efficient way to achieve this?

Upvotes: 3

Views: 1532

Answers (2)

user3483203
user3483203

Reputation: 51155

This is where + rank. Make sure you specify ascending=False or you'll get the incorrect output.


df['score'].where(df['condition'].eq(0)).rank(ascending=False)

A    3.0
B    2.0
C    NaN
D    1.0
E    4.0
Name: score, dtype: float64

Upvotes: 1

jezrael
jezrael

Reputation: 862661

You can filter by condition column rank:

df['new'] = df.loc[~df['condition'].astype(bool), 'score'].rank()
print (df)
   score  condition  new
A     34          0  2.0
B     65          0  3.0
C     12          1  NaN
D     98          0  4.0
E      5          0  1.0

Upvotes: 4

Related Questions