简体   繁体   中英

Use Python to Change csv Data Column Format

I am using python pandas to read csv file. The csv file has a datetime column that has second precisions "9/1/2015 9:25:00 AM", but if I open in excel, it has only minute precisions "9/1/15 9:25". Moreover, when I use the pd.read_csv() function, it only shows up to minute precision. Is there any way that I could solve the problem using python? Thanks much in advance.

This is a time formatting problem/philosophy by Excel. For some reason, Microsoft prefers to hide seconds and sub-seconds on user displays: even MSDOS's dir command omitted seconds.

If I were you, I'd use Excel's format operation and set it to display seconds, then save the spreadsheet as CSV and see if it put anything in it to record the improved formatting.

If that doesn't work, you might explore creating a macro which does the formatting, or use one of the IPC to Excel to command it to do your bidding.

Go in to "Format Cells" and enter d/mm/yyyy h:mm:ss as your custom formatting option

在此处输入图片说明

Edit: Not sure if I totally understand your question. Try this: create a text file with a single line "9/1/2015 9:25:00 AM", call it test.csv . Now do df = pd.read_csv('test.csv', header=None) . If you print your column with df[0] you should see this:

In [35]: df[0]
Out[35]:
0    9/1/2015 9:25:00 AM
Name: 0, dtype: object

ie Pandas has just read the column as a string (object) and the seconds are right there.

If you want to convert to a timestamp, call pd.to_datetime

In [36]: pd.to_datetime(df[0])
Out[36]:
0   2015-09-01 09:25:00
Name: 0, dtype: datetime64[ns]

Again the seconds are still there. Does this answer your question?

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