Advertisement
elena1234

convert column in df into DateTime format in Python

Sep 26th, 2022
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | Source Code | 0 0
  1. import pandas as pd
  2. from dateutil import parser
  3.  
  4. 1.Create list:
  5. column_date = list(df_jobs['Date'])
  6. column_date
  7.  
  8. 2.Data Cleaning
  9. new_column_date = []
  10. for item in column_date:
  11.     day = str(item[0]).rstrip()
  12.     month = str(item[1]).replace(',','')
  13.     new_column_date.append(f"{day}-{month}")
  14. new_column_date
  15.  
  16. df_jobs['Date'] = new_column_date
  17.  
  18.   #delete rows contain "today"
  19. df_jobs = df_jobs[~df_jobs.Date.str.contains("today", na=False)]
  20. df_jobs
  21.  
  22. 3.Parse string to DateTime:
  23. df_jobs['Date'] = [parser.parse(x) for x in df_jobs['Date']]
  24. df_jobs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement