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:
JavaScript
x
12
12
1
import pandas as pd
2
import datetime
3
4
actual_date = datetime.now()
5
actual_date = datetime.strptime("{}/{}/{}".format('01', actual_date.month, actual_date.year), "%d/%m/%Y")
6
7
def set_date(actual_date):
8
result = actual_date - dateutil.relativedelta.relativedelta(months=1)
9
print(datetime.strftime(result, "%d/%m/%Y"))
10
11
df['date'] = df.apply(lambda x: set_date(x[actual_date]), axis=1)
12
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
:
JavaScript
1
2
1
df['date']= df.apply(lambda x: set_date(actual_date), axis=1)
2