我在python数据框中有一个日期列。我想通过订购日期来索引这些。这是python中的可能吗?
date indexed
2007-02-21 3
2007-02-18 1
2007-02-24 5
2007-02-18 1
2007-02-23 4
2007-02-20 2
2007-02-23 4
我正在寻找索引,但我想我正在使用错误的术语来检查。请指导。
修改
实际上我想用等效的索引号替换日期。
答案 0 :(得分:1)
您要查找的是按日期排序的值_
df = pd.DataFrame(["2007-02-21","2007-02-18","2007-02-24","2007-02-18","2007-02-23","2007-02-20","2007-02-23"],columns=["date"])
df.sort_values("date", axis=0)
答案 1 :(得分:1)
您希望使用pd.factorize()方法的IIUC:
In [190]: df['new'] = pd.factorize(df['date'], sort=True)[0] + 1
In [191]: df
Out[191]:
date indexed new
0 2007-02-21 3 3
1 2007-02-18 1 1
2 2007-02-24 5 5
3 2007-02-18 1 1
4 2007-02-23 4 4
5 2007-02-20 2 2
6 2007-02-23 4 4
PS pd.factorize()
从0
开始计算,因此我已添加1
以满足您的预期结果
答案 2 :(得分:1)
import pandas as pd
df = pd.DataFrame(['2007-02-21','2007-02-18','2007-02-24','2007-02-18','2007-
02-23', '2007-02-20' , '2007-02-23'], index=[3, 1, 5, 1, 4,2,4], columns=
['Date'])
print df
Date
3 2007-02-21
1 2007-02-18
5 2007-02-24
1 2007-02-18
4 2007-02-23
2 2007-02-20
4 2007-02-23
df2 = df.sort_index(axis=0)
print(df2)
Date
1 2007-02-18
1 2007-02-18
2 2007-02-20
3 2007-02-21
4 2007-02-23
4 2007-02-23
5 2007-02-24