Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to flatten a column containing a list of dates as strings into just date?

I have a dataframe containing a column that has a list of dates stored as strings:

# sample dataframe
data = [[1, ["2019-08-02 08:30:56"]], [2, ["2020-08-02 08:30:56"]]]

df = pd.DataFrame(data, columns=["items", "dates"])
df["dates"] = df["dates"].astype(str)
df
  items                  dates
0   1   ['2019-08-02 08:30:56']
1   2   ['2020-08-02 08:30:56']

I would like to do several things:

  • Convert from a list to a string.
  • Convert the string to a date.
  • Eliminate the time stamp.

So that the final dataset would look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    items       dates
0   1      2019-08-02
1   2      2020-08-02

I am able to remove the list brackets by doing this:

df["dates_2"] = df["dates"].apply(lambda x: x[1:-1])

But I am wondering if there is a better way to do all of these things in one step?

>Solution :

Another solution is to apply ast.literal_eval to convert the column to list. Then .explode() it, convert to datetime and get a date part:

from ast import literal_eval

df["dates"] = df["dates"].apply(literal_eval)
df = df.explode("dates")
df["dates"] = pd.to_datetime(df["dates"]).dt.date

print(df)

Prints:

   items       dates
0      1  2019-08-02
1      2  2020-08-02
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading