Home > front end >  Check for previous value condition, and grabbing sucessor value with condition
Check for previous value condition, and grabbing sucessor value with condition

Time:01-20

I have the following sample series

s = {0: 'feedback ratings-positive-unexpected origin',
 1: 'decision-tree identified-regex input',
 2: 'feedback ratings-options input',
 3: 'feedback ratings-options-unexpected origin',
 4: 'checkout order-placed input',
 5: 'decision-tree identified-regex input'}

What I want to do is grab the values, that are under the "unexpected" keyword string and have the "input" string in them. So for example if I have 'feedback ratings-positive-unexpected origin', and the next value contains the "input" string. The map marks as True. So in this case I want to map 'decision-tree identified-regex input', and 'checkout order-placed input'.

The wanted map, would be something like this

want = {0: False,
 1: True,
 2: False,
 3: False,
 4: True,
 5: False}

I did the following map using looping, I was wondering if there was way of using pandas library.

mapi = []
for i in np.arange(s.shape[0]):
    if 'input' in s.iloc[i] and 'unexpected' not in s.iloc[i]:
        if 'unexpected' in s.iloc[i-1]:
            mapi.append(True)
        else:
            mapi.append(False)
    else:
        mapi.append(False)

CodePudding user response:

Use Series.str.contains chained with shifted values by Series.shift:

s = pd.Series(s)

m = s.str.contains('unexpected')
d = ((s.str.contains('input') & ~m) & m.shift(fill_value=False)).to_dict()
print (d)
{0: False, 1: True, 2: False, 3: False, 4: True, 5: False}

How it working:

m = s.str.contains('unexpected')

print (pd.concat([s.str.contains('input'),
                  s.str.contains('unexpected'),
                  m.shift(fill_value=False),
                  (s.str.contains('input') & ~m),
                  ((s.str.contains('input') & ~m) & m.shift(fill_value=False))], 
                 axis=1,
                 keys=['input','unexpected','shifted','both','final']))

   input  unexpected  shifted   both  final
0  False        True    False  False  False
1   True       False     True   True   True
2   True       False    False   True  False
3  False        True    False  False  False
4   True       False     True   True   True
5   True       False    False   True  False
  • Related