我有以下数据集:
PID,RUN_START_DATE,PUSHUP_START_DATE,SITUP_START_DATE,PULLUP_START_DATE
1,2013-01-24,2013-01-02,,2013-02-03
2,2013-01-30,2013-01-21,2013-01-13,2013-01-06
3,2013-01-29,2013-01-28,2013-01-01,2013-01-29
4,2013-02-16,2013-02-12,2013-01-04,2013-02-11
5,2013-01-06,2013-02-07,2013-02-25,2013-02-12
6,2013-01-26,2013-01-28,2013-02-12,2013-01-10
7,2013-01-26,,2013-01-12,2013-01-30
8,2013-01-03,2013-01-24,2013-01-19,2013-01-02
9,2013-01-22,2013-01-13,2013-02-03,
10,2013-02-06,2013-01-16,2013-02-07,2013-01-11
我知道我可以使用numpy.argsort
返回值的排序索引:
SQ_AL_INDX = numpy.argsort(df_sequence[['RUN_START_DATE', 'PUSHUP_START_DATE', 'SITUP_START_DATE', 'PULLUP_START_DATE']], axis=1)
...返回...
RUN_START_DATE PUSHUP_START_DATE SITUP_START_DATE PULLUP_START_DATE
0 2 1 0 3
1 3 2 1 0
2 2 1 0 3
3 2 3 1 0
4 0 1 3 2
5 3 0 1 2
6 1 2 0 3
7 3 0 2 1
8 3 1 0 2
9 3 1 0 2
但是,似乎将pandas.NaT
值放在第一个位置。因此,在此示例where PID == 1
中,排序顺序返回2 1 0 3
。但是,第二个索引位置是pandas.Nat
值。
如何在跳过pandas.NaT
值的同时获取已排序的索引(例如,2 1 np.NaN 3
或2 1 pandas.NaT 3
的返回索引值为1 0 2
或更好PID 1
1}}而不是2 1 0 3
)?
答案 0 :(得分:2)
将numpy.argsort
传递给apply
方法,而不是直接使用它。这样,NaNs / NaTs持续存在。以你的例子:
In [2]: df_sequence[['RUN_START_DATE', 'PUSHUP_START_DATE', 'SITUP_START_DATE', 'PULLUP_START_DATE']].apply(numpy.argsort, axis=1)
Out[2]:
RUN_START_DATE PUSHUP_START_DATE SITUP_START_DATE PULLUP_START_DATE
0 1 0 NaN 2
(etc.)