我正在为客户创建代码,我想知道是否可以修改创建的代码索引的顺序。
import pandas as pd
data = {
'dates' : ['2018-01-02T11:55:54', '2018-01-02T13:31:52',
'2018-01-02T13:34:38', '2018-01-02T13:38:30',
'2018-01-02T13:44:06'],
'clients' : ['peter', 'arnold','beto', 'peter', 'brandon']
}
df = pd.DataFrame(data)
out:
dates clients
0 2018-01-02T11:55:54 peter
1 2018-01-02T13:31:52 arnold
2 2018-01-02T13:34:38 beto
3 2018-01-02T13:38:30 peter
4 2018-01-02T13:44:06 brandon
我尝试先按日期对数据集进行排序,然后创建分类代码索引,但这没有用...
df.dates = pd.to_datetime( df.dates )
#sorting df by date:
df.sort_values('dates', inplace = True)
df.clients = pd.Categorical( df.clients )
df['client_code'] = df.clients.cat.codes
我希望结果是按客户注册日期排序的“代码”索引,但事实并非如此。
预期结果:
dates clients client_code
0 2018-01-02 11:55:54 peter 0
1 2018-01-02 13:31:52 arnold 1
2 2018-01-02 13:34:38 beto 2
3 2018-01-02 13:38:30 peter 0
4 2018-01-02 13:44:06 brandon 3
结果:
dates clients client_code
0 2018-01-02 11:55:54 peter 3
1 2018-01-02 13:31:52 arnold 0
2 2018-01-02 13:34:38 beto 1
3 2018-01-02 13:38:30 peter 3
4 2018-01-02 13:44:06 brandon 2
请帮助,并感谢您阅读...