-1

The df is as shown below...

enter image description here

The below code can only rank one column in place. I would like to rank all columns and post the rank values in a separate df

df['rank_2020-06-23'] = df['2020-06-23'].rank(pct=True) print(df)

bobby666
  • 67
  • 6
  • Please [create a reproducible copy of the DataFrame with `df.iloc[:10, :10].to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246/how-to-provide-a-copy-of-your-dataframe-with-to-clipboard), [edit] the question, and paste the clipboard into a code block. Add code, errors, and data as text, not screenshots because [Stack Overflow Discourages Screenshots](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). It is likely the question will be down-voted. You are discouraging assistance because no one wants to retype your data or code. – Trenton McKinney Jun 23 '20 at 19:34
  • Just use `df_rank = df.rank(pct=True)`. – Scott Boston Jun 23 '20 at 19:39
  • 1
    sorry for the df picture, the code posted by scott works. I don't know how I missed it. Thank you. – bobby666 Jun 23 '20 at 19:44

1 Answers1

0

Something like that should work:

df_ranks=pd.concat([pd.DataFrame(df[col].rank(pct=True)) for col in df.columns], axis=1)

It's simply using your function in a list comprehension, storing the results in dataframes to get a list of dataframes:

list_df_ranks=[pd.DataFrame(df[col].rank(pct=True)) for col in df.columns]

Then merging into one:

df_ranks=pd.concat(list_df_ranks, axis=1)