iterate in all dataframe rows and start with 0

NumPy + Pandas solution below.

In case of ambiguity (several ff-strings in a row) leftmost occurance is taken. In case of absence (no ff-string in a row) NaN value is used.

Try it online!

import pandas as pd, numpy as np

df = pd.DataFrame({
    'user':    ['fftest',     'fwadmin',     'fshelpdesk3', 'no',   'ffone'],
    'access':  ['fwadmin',    'ffuser2',     'fwadmin',     'user', 'fftwo'],
    'station': ['fshelpdesk', 'fshelpdesk2', 'ffuser3',     'here', 'three'],

sv = df.values.astype(np.str)
ix = np.argwhere(np.char.startswith(sv, 'ff'))[::-1].T
df.loc[ix[0], 'UserID'] = pd.Series(sv[(ix[0], ix[1])]).str[2:].values



          user   access      station UserID
0       fftest  fwadmin   fshelpdesk   test
1      fwadmin  ffuser2  fshelpdesk2  user2
2  fshelpdesk3  fwadmin      ffuser3  user3
3           no     user         here    NaN
4        ffone    fftwo        three    one

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top