diedlist = df['Died'].tolist()
for item in diedlist:
if item == '(living)':
continue
print(item)
item = dt.datetime.strptime(item, ("%b %d, %Y"))
print(item)
The initial print(item)
prints out the date in the existing format MMM DD, YYYY.
The print(item)
after the strptime()
function prints out the date in the new date format ("%b %d, %Y")
, and returns a datetime object of YYYY-MM-DD
as expected.
However, upon printing the list outside the loop, the items appear in their old format once again, as if untouched.
thank you for your help in advance.
You need to put the item back in the list:
for index, item in enumerate(diedlist):
if item == '(living)':
continue
item = dt.datetime.strptime(item, ("%b %d, %Y"))
diedlist[index] = item