python - Rank elements in a data frame while keeping index -
i'm using following formula gather top 20 elements each row in data frame. works great dropping index column df_returns i'd keep them. using dates index in df_returns data frame , i'd have same dates corresponding new data in df_rank data frame.
df_rank = pd.dataframe({n: df_returns.t[col].nlargest(21).index.tolist() n, col in enumerate(df_returns.t)}).t for example, let's wanting top 3 following data frame:
b c d e 1/1/2014 5 4 6 8 1 2/1/2014 2 1 6 3 1 3/1/2014 8 2 3 5 1 the results i'm getting are:
0 d c 1 c d 2 d c the results i'd are:
1/1/2014 d c 2/1/2014 c d 3/1/2014 d c
you use set_index set index of new dataframe original one:
df_rank.set_index(df_returns.index)
Comments
Post a Comment