Last active
August 13, 2023 00:32
-
-
Save chapmanjacobd/37d61693add62adb66b085e4e095aba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rank_dataframe(df, ascending_cols=None): | |
if ascending_cols is None: | |
ascending_cols = [] | |
numeric_cols = df.select_dtypes(include=["number"]).columns | |
ranks = df[numeric_cols].apply( | |
lambda x: x.rank( | |
method="min", na_option="bottom", ascending=x.name in ascending_cols | |
) | |
) | |
scaled_ranks = (ranks - 1) / (len(ranks.columns) - 1) | |
scaled_df = df.iloc[scaled_ranks.sum(axis=1).sort_values().index] | |
return scaled_df.reset_index(drop=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment