I have a dataframe containing a column id associated with a column containing a list of dates. These columns can contain 1 or more dates, but they are not sorted. How can I sort each row of the column dates chronologically?
data = [
[
1,
[
"2017-12-06",
"2017-12-05",
"2017-12-06",
"2018-01-03",
"2018-01-04",
"2017-11-24",
],
],
[
2,
[
"2017-12-06",
"2017-12-05",
"2017-12-06",
"2019-01-03",
],
],
]
df = pd.DataFrame(data, columns=["id", "dates"])
df
id dates
1 [2017-12-06, 2017-12-05, 2017-12-06, 2018-01-03, 2018-01-04, 2017-11-24]
2 [2017-12-06, 2017-12-05, 2017-12-06, 2019-01-03]
what I want (dates sorted chronologically):
id dates
1 [2017-11-24, 2017-12-05, 2017-12-06, 2018-01-03, 2018-01-04]
2 [2017-12-05, 2017-12-06, 2019-01-03]
what I've tried (that has not worked):
f = lambda x: x.sort(key=lambda date: datetime.strptime(date, "%Y-%m-%d"))
df["dates"].apply(f)
(this returns all None)