python某列符合条件则全部_如果使用groupby方法满足另一列中的条件,则使用多列进行条件过滤 - python...

我有df;

ID YEART Commdate Cat Category

0 LVI6AE2 1993 2017-03-24 LVI6AE2_1 56

1 LVI6BE2 1994 2017-03-24 LVI6BE2_1 67

2 APJ5LEV 1975 2017-03-13 APJ5LEV_1 78

3 LQL0AE3 1986 2017-03-16 LQL0AE3_1 87

4 BLR3UEV 1982 2017-03-15 BLR3UEV_1 90

5 BRL1NEV 1981 2017-03-15 BRL1NEV_1 90

6 BRL1NEV 1981 2017-03-16 BRL1NEV_1 90

7 BRL1NEV 1981 2017-03-22 Ungrouped 190

8 BRL1NEV 1981 2017-03-17 Ungrouped 190

9 BRL1NEV 1981 2017-03-17 Ungrouped 190

10 BRL1NEV 1981 2017-03-22 Ungrouped 190

11 BRL1NEV 1981 2017-03-20 BRL1NEV_1 90

12 BRL1NEV 1981 2017-02-01 BRL1NEV_1 90

13 UEE6JSV 2000 2017-03-15 UEE6JSV_1 34

14 UGQ4VE2 1993 2014-07-25 UGQ4VE2_1 45

15 UTU6BE1 1986 2017-03-13 UTU6BE1_1 12

16 NVT 1999 2017-03-10 NVT_1 12

17 OTL3JE1 2001 2017-02-01 OTL3JE1_1 12

18 OTL5XS1 2003 2017-03-01 OTL5XS1_1 12

19 OTL6AE1 2001 2017-03-01 OTL6AE1_1 12

20 JVU6AE1 1999 2017-03-31 JVU6AE1_1 12

21 JVU6AE2 1993 2017-03-31 Ungrouped 120

我想仅在它们属于未分组的“猫”或类别> 100的情况下,计算每个组中具有相似的“ ID”和“ YEART”的最早的“提交”

我想出了以下几行

#To Datetime

df['Commdate'] =pd.to_datetime(df['Commdate'])

#groupby

df["EarliestD"] =df.groupby(['ID', 'YEART']).filter(lambda x : x['Category'].count()>=90)['Commdate'].min()

结果为“ EarliestD”返回“ NaT”

ID YEART Commdate Cat Category EarliestD

0 LVI6AE2 1993 2017-03-24 LVI6AE2_1 56 NaT

1 LVI6BE2 1994 2017-03-24 LVI6BE2_1 67 NaT

2 APJ5LEV 1975 2017-03-13 APJ5LEV_1 78 NaT

3 LQL0AE3 1986 2017-03-16 LQL0AE3_1 87 NaT

4 BLR3UEV 1982 2017-03-15 BLR3UEV_1 90 NaT

题;

1.如果满足不同列中的条件,是否可以有条件地使用多个列进行分组?

2.是否可以在def函数中调用多个条件groupby?

谢谢

参考方案

您可以使用布尔过滤器和groupby + transform:

# convert Commdate to datetime if necessary

df['Commdate'] = pd.to_datetime(df['Commdate'])

# calculate mask for splitting dataframe

cat_mask = (df['Cat'] == 'Ungrouped') | (df['Category'] > 100)

# groupby uncategorised / category > 100

df.loc[cat_mask, 'Commdate'] = df.loc[cat_mask].groupby(['ID', 'YEART'])['Commdate'].transform('min')

结果:

print(df)

ID YEART Commdate Cat Category

0 LVI6AE2 1993 2017-03-24 LVI6AE2_1 56

1 LVI6BE2 1994 2017-03-24 LVI6BE2_1 67

2 APJ5LEV 1975 2017-03-13 APJ5LEV_1 78

3 LQL0AE3 1986 2017-03-16 LQL0AE3_1 87

4 BLR3UEV 1982 2017-03-15 BLR3UEV_1 90

5 BRL1NEV 1981 2017-03-15 BRL1NEV_1 90

6 BRL1NEV 1981 2017-03-16 BRL1NEV_1 90

7 BRL1NEV 1981 2017-03-17 Ungrouped 190

8 BRL1NEV 1981 2017-03-17 Ungrouped 190

9 BRL1NEV 1981 2017-03-17 Ungrouped 190

10 BRL1NEV 1981 2017-03-17 Ungrouped 190

11 BRL1NEV 1981 2017-03-20 BRL1NEV_1 90

12 BRL1NEV 1981 2017-02-01 BRL1NEV_1 90

13 UEE6JSV 2000 2017-03-15 UEE6JSV_1 34

14 UGQ4VE2 1993 2014-07-25 UGQ4VE2_1 45

15 UTU6BE1 1986 2017-03-13 UTU6BE1_1 12

16 NVT 1999 2017-03-10 NVT_1 12

17 OTL3JE1 2001 2017-02-01 OTL3JE1_1 12

18 OTL5XS1 2003 2017-03-01 OTL5XS1_1 12

19 OTL6AE1 2001 2017-03-01 OTL6AE1_1 12

20 JVU6AE1 1999 2017-03-31 JVU6AE1_1 12

21 JVU6AE2 1993 2017-03-31 Ungrouped 120

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值