简体   繁体   English

根据条件获取 Dataframe 中的最后一条和倒数第三条记录

[英]Getting the Last and 3rd Last Records in a Dataframe Based on Criteria

I have a large dataframe (extract below) and want to create a new dataframe containing the last "In-progress" row and the 3rd last "In-progress" row based on the Time for each ID.我有一个大的 dataframe(下面摘录),我想创建一个新的 dataframe,其中包含基于每个 ID 的时间的最后一个“进行中”行和倒数第三个“进行中”行。

I am new to Pandas and can't work out how to do it.我是 Pandas 的新手,不知道该怎么做。 Any help would be appreciated.任何帮助,将不胜感激。

Dataframe: Dataframe:

Time时间 State State ID ID Ref参考 Name姓名
10:00 10:00 In-progress进行中 54887 54887 1 1个 Jim吉姆
10:00 10:00 In-progress进行中 54887 54887 2 2个 Jon乔恩
10:00 10:00 In-progress进行中 54887 54887 3 3个 Rob
10:00 10:00 In-progress进行中 54887 54887 4 4个 Sam山姆
11:00 11:00 In-progress进行中 54887 54887 1 1个 Jim吉姆
11:00 11:00 In-progress进行中 54887 54887 2 2个 Jon乔恩
11:00 11:00 In-progress进行中 54887 54887 3 3个 Rob
11:00 11:00 In-progress进行中 54887 54887 4 4个 Sam山姆
12:00 12:00 In-progress进行中 54887 54887 1 1个 Jim吉姆
12:00 12:00 In-progress进行中 54887 54887 2 2个 Jon乔恩
12:00 12:00 In-progress进行中 54887 54887 3 3个 Rob
12:00 12:00 In-progress进行中 54887 54887 4 4个 Sam山姆
13:00 13:00 Done完毕 54887 54887 1 1个 Jim吉姆
13:00 13:00 Done完毕 54887 54887 2 2个 Jon乔恩
13:00 13:00 Done完毕 54887 54887 3 3个 Rob
13:00 13:00 Done完毕 54887 54887 4 4个 Sam山姆
10:00 10:00 In-progress进行中 65228 65228 a一种 Anya安雅
10:00 10:00 In-progress进行中 65228 65228 b b Lot很多
10:00 10:00 In-progress进行中 65228 65228 c c Ted泰德
10:00 10:00 In-progress进行中 65228 65228 d d Tom汤姆
11:00 11:00 In-progress进行中 65228 65228 a一种 Anya安雅
11:00 11:00 In-progress进行中 65228 65228 b b Lot很多
11:00 11:00 In-progress进行中 65228 65228 c c Ted泰德
11:00 11:00 In-progress进行中 65228 65228 d d Tom汤姆
12:00 12:00 In-progress进行中 65228 65228 a一种 Anya安雅
12:00 12:00 In-progress进行中 65228 65228 b b Lot很多
12:00 12:00 In-progress进行中 65228 65228 c c Ted泰德
12:00 12:00 In-progress进行中 65228 65228 d d Tom汤姆
13:00 13:00 Done完毕 65228 65228 a一种 Anya安雅
13:00 13:00 Done完毕 65228 65228 b b Lot很多
13:00 13:00 Done完毕 65228 65228 c c Ted泰德
13:00 13:00 Done完毕 65228 65228 d d Tom汤姆

Desired Result:期望的结果:

Time时间 State State ID ID Ref参考 Name姓名
10:00 10:00 In-progress进行中 54887 54887 1 1个 Jim吉姆
10:00 10:00 In-progress进行中 54887 54887 2 2个 Jon乔恩
10:00 10:00 In-progress进行中 54887 54887 3 3个 Rob
10:00 10:00 In-progress进行中 54887 54887 4 4个 Sam山姆
12:00 12:00 In-progress进行中 54887 54887 1 1个 Jim吉姆
12:00 12:00 In-progress进行中 54887 54887 2 2个 Jon乔恩
12:00 12:00 In-progress进行中 54887 54887 3 3个 Rob
12:00 12:00 In-progress进行中 54887 54887 4 4个 Sam山姆
10:00 10:00 In-progress进行中 65228 65228 a一种 Anya安雅
10:00 10:00 In-progress进行中 65228 65228 b b Lot很多
10:00 10:00 In-progress进行中 65228 65228 c c Ted泰德
10:00 10:00 In-progress进行中 65228 65228 d d Tom汤姆
12:00 12:00 In-progress进行中 65228 65228 a一种 Anya安雅
12:00 12:00 In-progress进行中 65228 65228 b b Lot很多
12:00 12:00 In-progress进行中 65228 65228 c c Ted泰德
12:00 12:00 In-progress进行中 65228 65228 d d Tom汤姆

3rd last to last倒数第三个

Use groupby.tail :使用groupby.tail

out = (df[df['State'].eq('In-progress')]
       .groupby(['Time', 'ID']).tail(3)
      )

Output: Output:

     Time        State     ID Ref Name
1   10:00  In-progress  54887   2  Jon
2   10:00  In-progress  54887   3  Rob
3   10:00  In-progress  54887   4  Sam
5   11:00  In-progress  54887   2  Jon
6   11:00  In-progress  54887   3  Rob
7   11:00  In-progress  54887   4  Sam
9   12:00  In-progress  54887   2  Jon
10  12:00  In-progress  54887   3  Rob
11  12:00  In-progress  54887   4  Sam
17  10:00  In-progress  65228   b  Lot
18  10:00  In-progress  65228   c  Ted
19  10:00  In-progress  65228   d  Tom
21  11:00  In-progress  65228   b  Lot
22  11:00  In-progress  65228   c  Ted
23  11:00  In-progress  65228   d  Tom
25  12:00  In-progress  65228   b  Lot
26  12:00  In-progress  65228   c  Ted
27  12:00  In-progress  65228   d  Tom

last and 3rd last (excluding 2nd last)最后和倒数第三(不包括倒数第二)

Use groupby.cumcount :使用groupby.cumcount

idx = (df[df['State'].eq('In-progress')]
       .groupby(['Time', 'ID']).cumcount(ascending=False)
       .isin([0,2]).loc[lambda x: x]
       .index
      )

out = df.loc[idx]

Output: Output:

     Time        State     ID Ref Name
1   10:00  In-progress  54887   2  Jon
3   10:00  In-progress  54887   4  Sam
5   11:00  In-progress  54887   2  Jon
7   11:00  In-progress  54887   4  Sam
9   12:00  In-progress  54887   2  Jon
11  12:00  In-progress  54887   4  Sam
17  10:00  In-progress  65228   b  Lot
19  10:00  In-progress  65228   d  Tom
21  11:00  In-progress  65228   b  Lot
23  11:00  In-progress  65228   d  Tom
25  12:00  In-progress  65228   b  Lot
27  12:00  In-progress  65228   d  Tom

get last and last-3 time per id获取每个 ID 的最后一次和最后 3 次

df1 = (df[df['State'].eq('In-progress')]
       .drop_duplicates(['ID', 'Time'])
       .groupby('ID')['Time'].nth([-3, -1]).reset_index())

df1

    ID      Time
0   54887   10:00
1   54887   12:00
2   65228   10:00
3   65228   12:00

filtering df by merge通过merge过滤df

df1.merge(df, how='left').reindex(columns=df.columns)

result:结果:

    Time    State       ID      Ref Name
0   10:00   In-progress 54887   1   Jim
1   10:00   In-progress 54887   2   Jon
2   10:00   In-progress 54887   3   Rob
3   10:00   In-progress 54887   4   Sam
4   12:00   In-progress 54887   1   Jim
5   12:00   In-progress 54887   2   Jon
6   12:00   In-progress 54887   3   Rob
7   12:00   In-progress 54887   4   Sam
8   10:00   In-progress 65228   a   Anya
9   10:00   In-progress 65228   b   Lot
10  10:00   In-progress 65228   c   Ted
11  10:00   In-progress 65228   d   Tom
12  12:00   In-progress 65228   a   Anya
13  12:00   In-progress 65228   b   Lot
14  12:00   In-progress 65228   c   Ted
15  12:00   In-progress 65228   d   Tom

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据最后3个字符对文本进行排序 - Sort text based on last 3rd character 检索不均匀数据帧中的最后一行和最后一行 - retrieve Last and 3rd to last row in uneven dataframe 根据熊猫数据框第 3 列中的条件,按天分组的 2 列的加权平均值 - Weighted average, grouped by day, of 2 columns based on criteria in 3rd column of pandas dataframe 根据列中的值获取数据框组的第一行和最后一行 - Getting first and last rows of dataframe groups based on values in a column 我想获取.txt文件中每行的最后一个元素和第三个到最后一个元素 - I want to get the last element and the 3rd to the last element of each line in a .txt file 如何根据其他两列中满足的条件索引最后一列中的 pandas DataFrame 元素? - How to index a pandas DataFrame element in last column based on criteria being met in two other columns? 获取数据框中每个循环的最后三个记录 - Get last three records for every loop in a dataframe 如何从一个代码中获取 => 最近的星期五、上周的星期五、上周的第二个星期五、最后一周的第三个星期五、最后一周的第 n 个星期五的日期? - How to get dates for => recent friday,friday of last week , friday of 2nd last week,friday of 3rd last wk ,friday of nth last wk from a single code? 如何将列表中的最后一个元素移动到 python 中列表的第三个 position? - How do I move my last element in the list to the 3rd position of the list in python? 根据条件的最后一次出现划分数据帧 - Divide a dataframe based on the last occurrence of a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM