gpt4 book ai didi

python - 在单元格条件下对 pandas 进行多个切片行

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:49 26 4
gpt4 key购买 nike

Msgtype Date ConvID   message
enquire 12/1 689 I want your car
reply 12/3 689 it is available
reply 12/4 689 rent please?
reply 12/6 689 $200
accept 12/8 689 please pay through CC
reply 12/8 689 thank you, what about fuel?
reply 12/8 689 you have to take care
enquire 12/3 690 Looking for car
reply 12/4 690 available
accept 12/5 690 paid
reply 12/6 690 thank you

我想按 ConvID 对这些数据进行分组并按日期排序。我想要直到“Msgtype” = 接受该特定 ConvID 为止的行。旨在分析消息数据,直到接受特定 ConvID 的预订请求。因此,对于 ConvID = 689,我想要行直到“Msgtype”= 接受。 “accept”之后的其余行不是必需的。

例如:ConvID = 689 不需要这两个

    Msgtype Date ConvID   message
reply 12/8 689 thank you, what about fuel?
reply 12/8 689 you have to take care

同样,ConvID = 690 不需要此行

Msgtype Date ConvID   message
reply 12/6 690 thank you

最佳答案

我认为你可以使用:

mask1 = (df.Msgtype == 'accept')
mask = mask1.groupby([df.ConvID]).apply(lambda x: x.shift().fillna(False).cumsum()) == 0

print (df[mask].sort_values(['ConvID','Date']))
Msgtype Date ConvID message
0 enquire 12/1 689 I want your car
1 reply 12/3 689 it is available
2 reply 12/4 689 rent please?
3 reply 12/6 689 $200
4 accept 12/8 689 please pay through CC
7 enquire 12/3 690 Looking for car
8 reply 12/4 690 available
9 accept 12/5 690 paid

说明:

#mask where is 'accept'
mask1 = (df.Msgtype == 'accept')
print (mask1)
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 False
8 False
9 True
10 False
Name: Msgtype, dtype: bool

#per group shift, replace NaN by False and cumulative sum
print (mask1.groupby([df.ConvID]).apply(lambda x: x.shift().fillna(False).cumsum()))
0 0
1 0
2 0
3 0
4 0
5 1
6 1
7 0
8 0
9 0
10 1
Name: Msgtype, dtype: int32
#where output of groupby is 0 
mask = mask1.groupby([df.ConvID]).apply(lambda x: x.shift().fillna(False).cumsum()) == 0
print (mask)
0 True
1 True
2 True
3 True
4 True
5 False
6 False
7 True
8 True
9 True
10 False
Name: Msgtype, dtype: bool

#boolean indexing and sorting
print (df[mask].sort_values(['ConvID','Date']))
Msgtype Date ConvID message
0 enquire 12/1 689 I want your car
1 reply 12/3 689 it is available
2 reply 12/4 689 rent please?
3 reply 12/6 689 $200
4 accept 12/8 689 please pay through CC
7 enquire 12/3 690 Looking for car
8 reply 12/4 690 available
9 accept 12/5 690 paid

关于python - 在单元格条件下对 pandas 进行多个切片行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40347660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com