Panda_User
Panda_User

Reputation: 199

Pandas conditional ranking

I have a pandas DataFrame that looks like this:

dataframe

I would like to use the pd.DataFrame.rank(axis=1, ascending=False) feature to rank the dataframe subject to the condition that the value is greater than zero. That is, only values greater than zero should be ranked. All other values less than or equal to zero should be zero.

Can anyone offer some advice?

Thank you!

Upvotes: 2

Views: 1777

Answers (1)

piRSquared
piRSquared

Reputation: 294278

Try this:

df[df > 0].rank(axis=1, ascending=False)

Upvotes: 1

Related Questions