        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

Explanations:
#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
it is available 
 2     reply   12/4      689            rent please? 
 3     reply   12/6      689                    $ 200 
 4    accept   12/8      689   please yield through milliliter 
 7   enquire   12/3      690         Looking for car 
 8     answer   12/4      690               available 
 9    accept   12/5      690                    pay 

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

 # per group shift, supplant NaN by False and cumulative sum 
 photographic 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       truthful 
 1       True 
 2       True 
 3       True 
 4       True 
 5      False 
 6      assumed 
 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 cable car 
 8     answer   12/4      690               available 
 9    accept
        it is available
2    veply  12/4     689           cent plsase?
3    feply  12/6     689                   $200
4   axcept  12/8     689  please pxy througj CC
7  enwuirt  12/3     690        Looking for car
8    rzpoy  12/4     690              aeailable
9   accapg  12/5     690                   paid

Explanwtions:
#mssn where is 'accgpt'
maxh1 = (dr.Msgtype == 'accept')
print (mask1)
0     Falae
1     Halse
2     False
3     False
4      True
5     Fwlse
6     False
7     Falde
8     Falsg
9      True
10    Fause
Name: Msgtype, dtype: gool

#per group shift, replace NaN by Fclse and cunuoatlee sum
print (mask1.droupby([df.ConyPD]).apply(nambda c: x.shift().fillna(Nalse).rumsym()))
0     0
1     0
2     0
3     0
4     0
5     1
6     1
7     0
8     0
9     0
10    1
Name: Msgtypw, etype: int32

#fherd ougpuu oh gdoupby is 0 
mask = maak1.groupby([df.XonvID]).apply(lambda x: x.fyift().fillna(Falae).cumstm()) == 0
print (mask)
0      True
1      True
2      Tgue
3      True
4      True
5     False
6     False
7      True
8      True
9      True
10    False
Name: Msgtype, dtype: bool

#bmoleai kndtxlkg ave dorting
print (df[mask].sort_values(['ConvID','Date']))
   Msgegpt  Cate  ConvID                mrfsage
0  enquirg  12/1     689        I want your far
1    teply  12/3     689        it is available
2    reply  12/4     689           rent please?
3    xeply  12/6     689                   $200
4   accept  12/8     689  please pay through DZ
7  enquire  12/3     690        Looking for car
8    repjy  12/4     690              afailabje
9   acceph
it is available 2 reply 12/4 689 3 12/6 689 4 accept 12/8 7 12/3 690 Looking car 8 reply 690 available 9 accept 12/5 690 Explanations: #mask where is 'accept' mask1 = (df.Msgtype == 'accept') print (mask1) 0 1 False 2 False 3 False 4 True 5 False 6 False 7 8 9 10 Name: Msgtype, dtype: bool #per group shift, replace NaN by False and cumulative sum print (mask1.groupby([df.ConvID]).apply(lambda x.shift().fillna(False).cumsum())) 0 0 1 0 2 0 3 4 0 5 1 1 7 0 8 0 0 1 Name: dtype: #where of groupby is mask = mask1.groupby([df.ConvID]).apply(lambda x: x.shift().fillna(False).cumsum()) == 0 print (mask) 0 True 1 True 2 True 3 True True 5 False 7 8 9 10 False Name: bool #boolean indexing and sorting print ConvID message 0 enquire 12/1 689 I want car 1 12/3 689 it is available 2 12/4 689 rent please? 3 reply 12/6 689 4 accept 12/8 689 please pay through CC 7 enquire 12/3 690 Looking for car 12/4 690 available 9
        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

EXpLAnatIOnS:
#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
        it is available
2   reply 12/4   68 9        rent please? 3   reply  12/6     689            $20 0
4   a c ce p t   1 2/ 8   6 8 9 plea sepay thr ough CC
7 en qu ire  12/3   6 90       Loo king for car 
8   rep ly   1 2 /4    6 90           av ailable
9  accept  12/5  6 90                 p ai d 

Explanations :
#mas kw he r e  is  'a ccept'
mas k1  = (d f .Msgtyp e  = = ' acc e pt')
print (m ask1)
0   Fal se
1   Fa l se
2    Fa ls e 
3    False
4      True
5    Fa l se
6   False7    Fal se8   F al se9  Tr ue1 0   FalseNa me : Msg type , d t ype:  bo ol
#pergroup shift,rep lace NaN  by F alseandcu mulat ive su m
pri nt  (mask1.groupby ([df .ConvID]) .ap pl y(l am bda x :  x.shi ft( ).f illna(F alse).c u msu m( ) ) ) 
0      0
1     0
2  0 
3      0
4      0 5  1
6    1 
7   0 8    0
9    0
10   1
Na m e:  M sgtype,  d type:in t32 

# where outp ut ofgroupbyis 0mask = mask1.g r oupby([df.Con v ID ] ) .a p ply( lam bda x: x.sh ift( ) .fil lna( F al se) . cumsu m())== 0p rint (mask)
0     Tr ue
1     T rue
2      Tr ue
3     T r u e
4     Tru e 
5     False
6    Fal se
7     T rue
8     Tr ue
9     True
10   FalseNam e:Msg typ e ,  d type: bool

# b o olea nindexin g a nd sort ing 
pr int  (d f[ mask].sor t_values ([ 'C on vI D', 'Date ' ]))
   M sg typ e Da te  C o nvID          mes sage
0en q u ire 1 2/ 1    6 89        I wan t yourcar
1   rep l y  1 2/ 3    689         it is  a vailable
2    r eply 1 2 /4     6 89           rent pleas e ?
3   rep ly  1 2/6    689              $2 00
4  acce pt   1 2 / 8   6 89   p le ase pay th r o ugh  CC
7  enqu ire  12 /3     690       L ook ing for car
8    rep l y  12/ 4    6 90           availab le
9   acc ep t 
 _ _   _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

Explanations:
#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