0

For instance I have a data frame that includes 'datetime' as index and column 'a' as variable. I wanted to get information for each row, on which quantile they fall into, for example I want to use 4 level of categories like below, on specific date only.

So in the 'category' column, it will show that row 1 falls into first category on 2019-05-02 data.

datetime                a      category
2019-05-02 12:00       10         1
2019-05-02 13:00       10         1
2019-05-02 14:00       15         2
2019-05-02 15:00       20         4
2019-05-02 16:00       10         1
2019-05-02 17:00       15         3
2019-05-03 18:00       ...       

Please don't mind the calculation. How do I define function to create the new column? Thanks!

npm
  • 643
  • 5
  • 17

1 Answers1

0

Found how:

data['category'] = data.groupby(pd.Grouper(freq='D'))['a'].transform(
                 lambda x: pd.qcut(x, 4, labels=range(1,5)))

Source: Pandas groupby and qcut

npm
  • 643
  • 5
  • 17