获得最后一行,可以使用pandas groupby满足条件

发布于 2025-02-02 15:09:14 字数 5059 浏览 5 评论 0原文

类似

数据一个
​:181/1/2200 00:00:004529539122,575366,4669.99%1/1/
1/1 00:00:0011/3/2022 15:31:18 4529541/1/1/1/
11/3/2022 15:31:181/1/2200 00:00:004529549122,575366,4669.99%
1/ 1/1/1/1/1/1900 00:00:00:00 21/7/2021 16:30:46452961 4529616170,984024,7153.33%
7/2021 16:30:4611/3/2022 15:31:0945296153126170,9821 /
13.9813.98 00:00:004529619122,57531271.74%1/1
/1/1/1/1/1/ 100:0013/10/2021 14:39:558012864052,1 1332,11332,8204.03%13403%13/
10/2021/2021 14:39: 5513/10/2021 14:43:098012864,47324052,199.89%
13/10/2021 14:43:093/2/2/2/2022 17:16:238012864473,2 4473,24,473299900.00%
3 /2/2022 17:16:231/1/2200 00:00:008012864946,84473,210.59%

我需要检查每个项目_ID,并获取COST_VAR IS的最后一行。如果是最后一行,那没关系,但是如果有一个,则是< 60%,我必须丢弃最后一行60%。输出应该看起来像这样:

date_fromdate_toitem_idvalue_newvalue_oldcost_var
11/3/2022 15:31:181/1/1/1/2200 00:00:004529539122,57 5366,575366,4669.99%69.99%11/3/2022
15:31:3/2022 15:31: 181/1/2200 00:00:004529549122,575366,4669.99%
11/3/2022 15:31:091/1/1/1/2200 00:00:004529619122,57531271.74%

5312 71.74% 802186 802186没有价值,因为最后一行> 60%(99900.00%)的下一行和cost_var< 60%(10.59%)...是否可以这样做?我找不到解决方法。

I have a DataFrame like this:

date_fromdate_toitem_idVALUE_NEWVALUE_OLDcost_var
1/1/1900 00:00:0011/3/2022 15:31:184529535366,464024,7133.34%
11/3/2022 15:31:181/1/2200 00:00:004529539122,575366,4669.99%
1/1/1900 00:00:0011/3/2022 15:31:184529545366,464024,7133.34%
11/3/2022 15:31:181/1/2200 00:00:004529549122,575366,4669.99%
1/1/1900 00:00:0021/7/2021 16:30:464529616170,984024,7153.33%
21/7/2021 16:30:4611/3/2022 15:31:0945296153126170,9813.92%
11/3/2022 15:31:091/1/2200 00:00:004529619122,57531271.74%
1/1/1900 00:00:0013/10/2021 14:39:558012864052,11332,8204.03%
13/10/2021 14:39:5513/10/2021 14:43:098012864,47324052,199.89%
13/10/2021 14:43:093/2/2022 17:16:238012864473,24,473299900.00%
3/2/2022 17:16:231/1/2200 00:00:008012864946,84473,210.59%

I need to check each item_id, and get the last row where cost_var is >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%. Output should look like this:

date_fromdate_toitem_idVALUE_NEWVALUE_OLDcost_var
11/3/2022 15:31:181/1/2200 00:00:004529539122,575366,4669.99%
11/3/2022 15:31:181/1/2200 00:00:004529549122,575366,4669.99%
11/3/2022 15:31:091/1/2200 00:00:004529619122,57531271.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? I couldn't find a way to solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们可以使用groupby选择每个item_id的最后一行,并且只选择其中cost_var&gt; 60%使用查询

df.groupby('item_id', as_index=False).last().query("cost_var.str.rstrip('%').astype('float')>60")

We can select the last row of each item_id using groupby and only select ones where cost_var > 60% using query.

df.groupby('item_id', as_index=False).last().query("cost_var.str.rstrip('%').astype('float')>60")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文