简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM