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.
pandas.DataFrame.apply?pd.Seriesbut 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 usingdf['Col1'].apply(_my_func)) you'd need to maintain all the 'yes' and 'no' results into an iterable (list or pd.Series)