3

How can I use .loc with .str.match() to update column values but with a function call? The code I'm trying is like;

df.loc[df['Col1'].str.match(r'\d\d/\d\d/\d\d\d\d', na=False), 'Col2'] = _my_func(df['Col1'])

a simple regex pattern to find date format, and then _myfunc();

def _my_func(data)
    for row in data.iteritems():
        day = int(row[1][:2])
        month = int(row[1][3:5])
        year = int(row[1][6:])
        fecha = datetime.datetime(year, month, day, 0, 0, 0)
        diff =  fecha - datetime.datetime.now()
        if diff.days > 0:
            return 'Yes'
        elif diff.days < 0:
            return 'No'

Is this a correct way to return values from the function into the dataframe?

Also if I insert a print('test') into the _my_func just before either return, it only prints test one time, instead of a print for each row in the data passed to the function, does anyone know why? Thank you.

2
  • 1
    have you looked at pandas.DataFrame.apply ? Commented Oct 1, 2018 at 11:59
  • 1
    Your function takes a pd.Series but returns just one yes or no answer. This would be why your print test only occurs once. For this to work using this method (i.e. instead of using df['Col1'].apply(_my_func)) you'd need to maintain all the 'yes' and 'no' results into an iterable (list or pd.Series) Commented Oct 1, 2018 at 11:59

2 Answers 2

2

You can try it using apply() function.

For example:

df['loc1'] = df['loc1'].apply(_my_func)

Then it would take each row of the dataframe and pass it as input to the function _my_func.

Sign up to request clarification or add additional context in comments.

Comments

1

Following my comment:

def _my_func(x):
    day = int(x[:2])
    month = int(x[3:5])
    year = int(x[6:])
    fecha = datetime.datetime(year, month, day, 0, 0, 0)
    diff = fecha - datetime.datetime.now()
    if diff.days > 0:
        return 'Yes'
    elif diff.days < 0:
        return 'No'

Followed by:

df.loc[df['Col1'].str.match(r'\d\d/\d\d/\d\d\d\d', na=False), 'Col2'] = df['Col1'].apply(_my_func)

1 Comment

Exactly what I needed, I edited out the first index in your answer because it wasn't needed after making the apply changes, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.