0

If I have created a predefined list, say top 3 stocks in the order of ["CAT","AI", "BOC"], how can I get this new dataframe by working from the original dataframe below that will essentially sort_value the ticker column by the order for which the above predefined list is shown? (still chronological order also)

Thanks

     date       price ticker  volume
0  2018-01-01  1.323     AI    2000
1  2018-01-02  1.525     AI    1500
2  2018-01-03  1.045     AI     500
3  2018-01-01  2.110    BOC    3201
4  2018-01-02  2.150    BOC    5200
5  2018-01-03  2.810    BOC    1980
6  2018-01-01  5.199    CAT    2000
7  2018-01-02  4.980    CAT     450
8  2018-01-03  4.990    CAT    3000
Brian
  • 2,163
  • 1
  • 14
  • 26
cqstack
  • 155
  • 1
  • 8
  • And see the docs: https://pandas.pydata.org/pandas-docs/stable/categorical.html#multi-column-sorting – root Apr 03 '18 at 21:53

1 Answers1

1

One option could be to create an ordered Categorical column, apply argsort on it which returns the correct indices to reorder your data frame:

df.iloc[pd.Categorical(df.ticker, categories=["CAT","AI", "BOC"], ordered=True).argsort()]

#         date  price ticker  volume
#6  2018-01-01  5.199    CAT    2000
#7  2018-01-02  4.980    CAT     450
#8  2018-01-03  4.990    CAT    3000
#0  2018-01-01  1.323     AI    2000
#1  2018-01-02  1.525     AI    1500
#2  2018-01-03  1.045     AI     500
#3  2018-01-01  2.110    BOC    3201
#4  2018-01-02  2.150    BOC    5200
#5  2018-01-03  2.810    BOC    1980
Psidom
  • 209,562
  • 33
  • 339
  • 356