john div
john div

Reputation: 15

python csv file add a comma on date and time

I trying to write a code using python with pandas for csv file. I am trying to add comma on this date set 20200625045500

for example table.csv on

 id_posts date_time   f_categories f_user_name  
 80   20200625045500  2CB    johntest
 81   20200725045500  2CaB    johntest
 82   20200805045500  2CsB    johntest
 83   20200725045500  2CdB    johntest
 84   20200625045500  2C4B    johntest

output:

 id_posts date_time     f_categories f_user_name  

 80   2020-07-25,04:55:00  2CB     johntest
 81   2020-09-25,04:55:00  2CaB    johntest
 82   2020-07-25,04:55:00  2CsB    johntest
 83   2020-01-20,04:55:00  2CdB    johntest
 84   2020-07-15,04:55:00  2C4B    johntest

import pandas as pd

data = pd.read_csv(r'table.csv')
print(data)

Upvotes: 0

Views: 239

Answers (1)

here it is with the code that will run over your dataset:

import pandas as pd
from datetime import datetime

data = pd.read_csv(r'table.csv')
data['date_time'] = data['date_time'].apply(lambda x: datetime.strptime(str(x), '%Y%m%d%H%M%S').strftime('%Y/%m/%d,%H:%M:%S'))
print(data)

Let me know if this was useful.

Upvotes: 1

Related Questions