简体   繁体   中英

Need to select rows of pandas.DataFrame by last 5 datetime(Timestamp)

I have a dataframe:

|-    |   USER        |        Timestamp     |

|   0 | A    | 2019-10-01 08:32:29.608000    |

|   1 | A    | 2019-10-01 08:32:34.541000    |

|   2 | A    | 2019-10-01 08:32:35.863001    |

|   3 | A    | 2019-10-01 08:32:35.864002    |

|   4 | A    | 2019-10-01 08:32:36.398003    |

|   5 | A    | 2019-10-01 08:32:39.517000    |

|   6 | A    | 2019-10-01 08:32:39.567005    |

|   7 | A    | 2019-10-01 08:32:41.039000    |

 ...

| 130 | B    | 2019-10-01 22:12:21.966022    |

| 131 | B    | 2019-10-01 22:12:23.549023    |

| 132 | B    | 2019-10-01 22:12:24.977024    |

| 133 | B    | 2019-10-01 22:12:25.922025    |

| 134 | B    | 2019-10-01 22:12:26.935026    |

| 135 | B    | 2019-10-01 22:12:28.487027    |

| 136 | B    | 2019-10-01 22:12:29.593028    |

| 137 | B    | 2019-10-01 22:12:31.926029    |

from dataframe I neet to leave only rows of last 5 Timestamps for every USER .

I have tried indexing, changing dtype to datetime64[ns].

Here what I expect: ONLY 5 last timestamp for every user

|     | USER |           Timestamp           |

|   3 | A    | 2019-10-01 08:32:35.864002    |

|   4 | A    | 2019-10-01 08:32:36.398003    |

|   5 | A    | 2019-10-01 08:32:39.517000    |

|   6 | A    | 2019-10-01 08:32:39.567005    |

|   7 | A    | 2019-10-01 08:32:41.039000    |

| ... 

| 133 | B    | 2019-10-01 22:12:25.922025    |

| 134 | B    | 2019-10-01 22:12:26.935026    |

| 135 | B    | 2019-10-01 22:12:28.487027    |

| 136 | B    | 2019-10-01 22:12:29.593028    |

| 137 | B    | 2019-10-01 22:12:31.926029    |

PS Also you can mention timestamp in ascending order. I have thought tried using by index, unfortunately pandas type - object.

Use DataFrame.sort_values with GroupBy.tail :

df = df.sort_values('Timestamp')
df = df.groupby('USER').tail(5)
print (df)
    USER                  Timestamp
3      A 2019-10-01 08:32:35.864002
4      A 2019-10-01 08:32:36.398003
5      A 2019-10-01 08:32:39.517000
6      A 2019-10-01 08:32:39.567005
7      A 2019-10-01 08:32:41.039000
133    B 2019-10-01 22:12:25.922025
134    B 2019-10-01 22:12:26.935026
135    B 2019-10-01 22:12:28.487027
136    B 2019-10-01 22:12:29.593028
137    B 2019-10-01 22:12:31.926029

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM