简体   繁体   English

获取每个熊猫 cloumn 值的最后条件值

[英]Get Last condition value for each pandas cloumn value

I have a Df like this:我有一个像这样的 Df:

date_from date_from date_to date_to item_id item_id VALUE_NEW VALUE_NEW VALUE_OLD VALUE_OLD cost_var成本变量
1/1/1900 00:00:00 1900 年 1 月 1 日 00:00:00 11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 452953 452953 5366,46 5366,46 4024,71 4024,71 33.34% 33.34%
11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452953 452953 9122,57 9122,57 5366,46 5366,46 69.99% 69.99%
1/1/1900 00:00:00 1900 年 1 月 1 日 00:00:00 11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 452954 452954 5366,46 5366,46 4024,71 4024,71 33.34% 33.34%
11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452954 452954 9122,57 9122,57 5366,46 5366,46 69.99% 69.99%
1/1/1900 00:00:00 1900 年 1 月 1 日 00:00:00 21/7/2021 16:30:46 21/7/2021 16:30:46 452961 452961 6170,98 6170,98 4024,71 4024,71 53.33% 53.33%
21/7/2021 16:30:46 21/7/2021 16:30:46 11/3/2022 15:31:09 2022 年 11 月 3 日 15:31:09 452961 452961 5312 5312 6170,98 6170,98 13.92% 13.92%
11/3/2022 15:31:09 2022 年 11 月 3 日 15:31:09 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452961 452961 9122,57 9122,57 5312 5312 71.74% 71.74%
1/1/1900 00:00:00 1900 年 1 月 1 日 00:00:00 13/10/2021 14:39:55 13/10/2021 14:39:55 801286 801286 4052,1 4052,1 1332,8 1332,8 204.03% 204.03%
13/10/2021 14:39:55 13/10/2021 14:39:55 13/10/2021 14:43:09 13/10/2021 14:43:09 801286 801286 4,4732 4,4732 4052,1 4052,1 99.89% 99.89%
13/10/2021 14:43:09 13/10/2021 14:43:09 3/2/2022 17:16:23 2022 年 3 月 2 日 17:16:23 801286 801286 4473,2 4473,2 4,4732 4,4732 99900.00% 99900.00%
3/2/2022 17:16:23 2022 年 3 月 2 日 17:16:23 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 801286 801286 4946,8 4946,8 4473,2 4473,2 10.59% 10.59%

I need to check each item_id, and get the last row where cost_var is >60%.我需要检查每个 item_id,并获取 cost_var > 60% 的最后一行。 If it's the last row, that's ok, but if there is a next one, and it is <60%, I have to drop the last row>60%.如果它是最后一行,那没关系,但如果有下一行,它 <60%,我必须删除最后一行>60%。 Output should look like this:输出应如下所示:

date_from date_from date_to date_to item_id item_id VALUE_NEW VALUE_NEW VALUE_OLD VALUE_OLD cost_var成本变量
11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452953 452953 9122,57 9122,57 5366,46 5366,46 69.99% 69.99%
11/3/2022 15:31:18 2022 年 11 月 3 日 15:31:18 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452954 452954 9122,57 9122,57 5366,46 5366,46 69.99% 69.99%
11/3/2022 15:31:09 2022 年 11 月 3 日 15:31:09 1/1/2200 00:00:00 2200 年 1 月 1 日 00:00:00 452961 452961 9122,57 9122,57 5312 5312 71.74% 71.74%

Item 802186 returned no value, because last row>60% (99900.00%) has a next row and cost_var<60% (10.59%)...Is it possible to do?项目 802186 没有返回任何值,因为最后一行>60% (99900.00%) 有下一行,并且 cost_var<60% (10.59%)...有可能吗? I couldn't find a way to solve it.我找不到解决它的方法。

Try this尝试这个

# read data
df = pd.read_clipboard()
# select the last row of each item_id and only select ones where cost_var > 60%
df.groupby(df.item_id, as_index=False).last().query("cost_var.str.rstrip('%').astype('float')>60", engine='python')

在此处输入图像描述

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

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