Advertisement
furas

pandas edit strings before datetime

Jun 29th, 2018
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. df = pd.DataFrame({
  4.     'text': [
  5.         ' 00:00:01        ',
  6.         '2018.06.29 00:00:01',
  7.         'Hello 01:00:00'
  8.     ],
  9. })
  10.  
  11. print(df)
  12.  
  13. df['time'] = df['text'].apply(lambda x: x.strip()[-8:])
  14.  
  15. print(df)
  16.  
  17. df['time'] = pd.to_datetime(df['time'], format='%H:%M:%S')
  18.  
  19. print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement