SHARE
TWEET

pandas edit strings before datetime

furas Jun 29th, 2018 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top