Skip to content

Instantly share code, notes, and snippets.

@Dih5
Created November 16, 2018 09:15
Show Gist options
  • Save Dih5/3b7f82aa9584eb5958b5d6bf50034405 to your computer and use it in GitHub Desktop.
Save Dih5/3b7f82aa9584eb5958b5d6bf50034405 to your computer and use it in GitHub Desktop.
Reformat a date in a CSV file
#!/usr/bin/env python3
import pandas as pd
original_format='%Y-%m-%d %H:%M:%S'
target_format='%d-%m-%y %H:%M'
attribute_name='Date'
file_path='ACertainFile.csv'
df=pd.read_csv(file_path)
df[attribute_name]=pd.to_datetime(df[attribute_name], format=original_format).apply(lambda x: x.strftime(target_format))
df.to_csv("cleaned-"+file_path,index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment