python - 使用 NaT 值对 Pandas 数据框进行排序

标签 python sorting pandas numpy

我正在尝试对顶部带有 NaT 值的 Pandas 数据框进行排序。我正在使用 df.sort_values 函数:

df=df.sort_values(by='date_of_last_hoorah_given')

它工作正常,我在底部得到一个带有 NaT 值的排序数据框

    date_of_last_hoorah_given                              email   first_name  \
16 2016-12-19 07:36:08.000000              mindy.lyndi@hoorah.io        Mindy   
29 2016-12-19 07:36:08.000000              judi.seward@hoorah.io         Judi   
7  2016-12-19 07:36:08.000000                  chao.an@hoorah.io         Chao   
21 2016-12-19 07:36:08.000000              bala.harish@hoorah.io         Bala   
12 2016-12-19 07:36:08.000000            pushpa.swaran@hoorah.io       Pushpa   
30 2016-12-22 07:36:08.000000       sparrow.freespirit@hoorah.io      Sparrow   
28 2016-12-22 07:36:08.000000         sanjeev.prasanna@hoorah.io      Sanjeev   
27 2016-12-22 07:36:08.000000     twinklenose.snowleaf@hoorah.io  Twinklenose   
25 2016-12-22 07:36:08.000000       sweetgaze.sugarshy@hoorah.io    Sweetgaze   
23 2016-12-22 07:36:08.000000            shreya.sarika@hoorah.io       Shreya   
19 2016-12-22 07:36:08.000000              jiahao.dong@hoorah.io       Jiahao   
15 2016-12-22 07:36:08.000000            jannine.tyson@hoorah.io       Janine   
14 2016-12-22 07:36:08.000000                arlo.reed@hoorah.io         Arlo   
0  2016-12-22 07:36:08.000000         aditya.hariharan@hoorah.io       Aditya   
11 2016-12-22 07:36:08.000000        shirley.madalitso@hoorah.io      Shirley   
2  2016-12-22 07:36:08.000000             minerva.jena@hoorah.io     Minerva    
3  2016-12-22 07:36:08.000000             colby.brandi@hoorah.io        Colby   
13 2016-12-22 07:36:08.000000            beverly.cohen@hoorah.io      Beverly   
6  2016-12-22 07:36:08.000000             guanting.jun@hoorah.io     Guanting   
5  2016-12-22 07:36:08.000000                  chen.tu@hoorah.io         Chen   
18 2016-12-22 10:55:03.474683                  fen.lin@hoorah.io          Fen   
9  2016-12-23 07:36:08.000000             kourtney.pam@hoorah.io     Kourtney   
10 2016-12-23 14:30:55.206581             kailee.alfie@hoorah.io       Kailee   
4  2016-12-24 07:36:08.000000                jing.chao@hoorah.io        Jing    
31 2016-12-24 16:02:28.945809               rich.oswin@hoorah.io         Rich   
24 2016-12-25 07:36:08.000000           ganesh.vasanta@hoorah.io       Ganesh   
8  2016-12-26 07:36:08.000000               xia.yaling@hoorah.io          Xia   
20 2016-12-27 07:36:08.000000              kinley.joan@hoorah.io       Kinley   
22 2016-12-28 07:36:08.000000   honeygleam.dazzlesmile@hoorah.io   Honeygleam   
26 2016-12-28 15:29:48.629929             indira.padma@hoorah.io       Indira   
17 2016-12-29 02:27:11.125078             ileen.gaynor@hoorah.io        Ileen   
32 2016-12-29 15:38:02.335296            ragnar.lestat@hoorah.io       Ragnar   
1                         NaT  flitterbeam.clovergaze@hoorah.com  Flitterbeam   

但是当我尝试使用以下代码将其放在首位时:

df=df.sort_values(by='date_of_last_hoorah_given',ascending=[1,0])

我得到一个 valueError: Length of ascending (2) != length of by (1) 完整的堆栈跟踪如下:

ValueError                                Traceback (most recent call last)
<ipython-input-107-948a8354aeeb> in <module>()
      1 cd = ClientData(1)
----> 2 cd.get_inactive_users()

<ipython-input-106-ed230054ea86> in get_inactive_users(self)
    346             inactive_users_result.append(user_dict)
    347         df=pd.DataFrame(inactive_users_result)
--> 348         df=df.sort_values(by='date_of_last_hoorah_given',ascending=[1,0])
    349         print(df)

C:\Users\aditya\Anaconda3\lib\site-packages\pandas\core\frame.py in sort_values(self, by, axis, ascending, inplace, kind, na_position)
   3126         if com.is_sequence(ascending) and len(by) != len(ascending):
   3127             raise ValueError('Length of ascending (%d) != length of by (%d)' %
-> 3128                              (len(ascending), len(by)))
   3129         if len(by) > 1:
   3130             from pandas.core.groupby import _lexsort_indexer

ValueError: Length of ascending (2) != length of by (1)

最佳答案

问题是 NaT 在排序时是最大的,因此总是排在最后。为了在将 NaT 放在前面或顶部的同时按升序日期排序,您需要使用两个条件进行排序。

np.lexsort 将按任意数量的条件对数组进行排序,并返回类似于 np.argsort

的排序切片

另请注意,我会将 notnull 条件放在传递给 np.lexsort 的条件数组的最后。 np.lexsort 首先对最后的元素进行排序...我不知道为什么,但就是这样。

所以我们应该首先按 df.date_of_last_hoorah_given.notnull() 排序,因为那些不为 null 的将具有 True 的值,该值大于 在排序上下文中为 False。然后我们可以按其余日期排序。

dates = df.date_of_last_hoorah_given
sort_slice = np.lexsort([dates.values, dates.notnull().values])
df.iloc[sort_slice]

或者!正如 OP 在评论中所说,这给出了同样的东西,并且更加直接

df.sort_values('date_of_last_hoorah_given', na_position='first')

     date_of_last_hoorah_given                              email   first_name
1                          NaT  flitterbeam.clovergaze@hoorah.com  Flitterbeam
16  2016-12-19 07:36:08.000000              mindy.lyndi@hoorah.io        Mindy
29  2016-12-19 07:36:08.000000              judi.seward@hoorah.io         Judi
7   2016-12-19 07:36:08.000000                  chao.an@hoorah.io         Chao
21  2016-12-19 07:36:08.000000              bala.harish@hoorah.io         Bala
12  2016-12-19 07:36:08.000000            pushpa.swaran@hoorah.io       Pushpa
30  2016-12-22 07:36:08.000000       sparrow.freespirit@hoorah.io      Sparrow
28  2016-12-22 07:36:08.000000         sanjeev.prasanna@hoorah.io      Sanjeev
27  2016-12-22 07:36:08.000000     twinklenose.snowleaf@hoorah.io  Twinklenose
25  2016-12-22 07:36:08.000000       sweetgaze.sugarshy@hoorah.io    Sweetgaze
23  2016-12-22 07:36:08.000000            shreya.sarika@hoorah.io       Shreya
19  2016-12-22 07:36:08.000000              jiahao.dong@hoorah.io       Jiahao
15  2016-12-22 07:36:08.000000            jannine.tyson@hoorah.io       Janine
14  2016-12-22 07:36:08.000000                arlo.reed@hoorah.io         Arlo
0   2016-12-22 07:36:08.000000         aditya.hariharan@hoorah.io       Aditya
11  2016-12-22 07:36:08.000000        shirley.madalitso@hoorah.io      Shirley
2   2016-12-22 07:36:08.000000             minerva.jena@hoorah.io      Minerva
3   2016-12-22 07:36:08.000000             colby.brandi@hoorah.io        Colby
13  2016-12-22 07:36:08.000000            beverly.cohen@hoorah.io      Beverly
6   2016-12-22 07:36:08.000000             guanting.jun@hoorah.io     Guanting
5   2016-12-22 07:36:08.000000                  chen.tu@hoorah.io         Chen
18  2016-12-22 10:55:03.474683                  fen.lin@hoorah.io          Fen
9   2016-12-23 07:36:08.000000             kourtney.pam@hoorah.io     Kourtney
10  2016-12-23 14:30:55.206581             kailee.alfie@hoorah.io       Kailee
4   2016-12-24 07:36:08.000000                jing.chao@hoorah.io         Jing
31  2016-12-24 16:02:28.945809               rich.oswin@hoorah.io         Rich
24  2016-12-25 07:36:08.000000           ganesh.vasanta@hoorah.io       Ganesh
8   2016-12-26 07:36:08.000000               xia.yaling@hoorah.io          Xia
20  2016-12-27 07:36:08.000000              kinley.joan@hoorah.io       Kinley
22  2016-12-28 07:36:08.000000   honeygleam.dazzlesmile@hoorah.io   Honeygleam
26  2016-12-28 15:29:48.629929             indira.padma@hoorah.io       Indira
17  2016-12-29 02:27:11.125078             ileen.gaynor@hoorah.io        Ileen
32  2016-12-29 15:38:02.335296            ragnar.lestat@hoorah.io       Ragnar

关于python - 使用 NaT 值对 Pandas 数据框进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478458/

相关文章:

Python selenium - 从 p 和 span 获取值

python - pysvn 基于日期导出

python - 从 Django 0.96 升级到 1.0 的最佳方法是什么?

php - 从 mysql 数据库中发出一个多词的排序

C#:使用匿名函数排序

python - 根据多个条件替换 Pandas 数据框中的值

python - 如何按周按日期时间分组

python - 如何在选中复选框时启用行编辑

java - 将文件内容复制到链表数组中并对其进行排序

python - 要指定列名称,为什么 `pd.read_csv` 和 `pd.DataFrame` 使用相同的参数名称?