I am trying to apply a function that returns an specific date in an specific format, however I am struggling to apply this function to a new pandas dataframe column.
Here’s what I got so far:
import pandas as pd import datetime actual_date = datetime.now() actual_date = datetime.strptime("{}/{}/{}".format('01', actual_date.month, actual_date.year), "%d/%m/%Y") def set_date(actual_date): result = actual_date - dateutil.relativedelta.relativedelta(months=1) print(datetime.strftime(result, "%d/%m/%Y")) df['date'] = df.apply(lambda x: set_date(x[actual_date]), axis=1)
The next error arises:
KeyError: datetime.datetime(2021, 2, 1, 0, 0)
Expected output could be a pandas dataframe column where row-values are set_date
output.
How could I accomplish this task?
Advertisement
Answer
The issue was related to the setting of x
for lambda
:
df['date']= df.apply(lambda x: set_date(actual_date), axis=1)