Struggling with .rank()

Hey Brian, I have a couple questions regarding rank in the example codes provided:

  1. I noticed rank doesn't "sort". For example
    ranked_companies = roic.rank(axis=1,ascending=False, pct=True)

My thought was that a call to 'rank' here would show the top ranked companies by their ROIC, but when you print out the dataframe, whether by itself or with stack(), it looks like no operation has been performed because the columns are not arranged by highest ranked company (highest value). How would you get the top 30 ranked stocks off this operation?

  1. If you wanted to rank on multiple factors, how would you do it? For example in Magic Formula Investing, (Magic formula investing - Wikipedia), they want you to rank stocks by ROIC and Earnings Yield. My solution was to (ROIC + Earnings_Yield) / 2 to get a combined ranking factor, but is there a better way to do this with pandas?

In the examples and in Moonshot you will generally preserve the original shape of your DataFrame and instead create Boolean masks indicating whether stocks meet your conditions:

daily_ranks = roic.rank(axis=1, ascending=False) # don't use pct=True if you want integer ranks
are_in_top_30 = daily_ranks <= 30

# then do something with it
signals = are_in_top_30.astype(int)

If you want to look at the top 30 interactively, you don't need rank, you need to pick a day, stack and sort.

latest_roic = roic.iloc[-1]
latest_roic.stack().sort_values(ascending=False).head(30)

Awesome, thank you, Brian!