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 can I convert an array of dates into a Pandas DataFrame?

I have a Python array in the form:

K = [ [2022,1,16], [2022,1,18], [2022,2,12], [2022,3,24]]

This array contains dates within sub-arrays.

How can I turn it into a Pandas DataFrame with 1 column of dates in standard format (%d/%m/%Y)?

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

>Solution :

import pandas as pd
date_array = [ [2022,1,16], [2022,1,18], [2022,2,12], [2022,3,24]]
date_df = pd.DataFrame(date_array, columns=['year', 'month', 'day'])
date_df['date'] = pd.to_datetime(date_df[['year', 'month', 'day']], format='%d/%m/%Y')

And if you’d like you can drop extra columns

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