3

我有以下数据集:

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 3or2 1 pandas.NaT 3或更好1 0 2PID 1而不是2 1 0 3)?

4

1 回答 1

2

传递numpy.argsortapply方法而不是直接使用它。这样,NaN/NaT 会持续存在。对于您的示例:

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.)
于 2013-03-04T18:49:23.813 回答