简体   繁体   中英

How to convert epoch time to another format and save it into csv file in Python?

How to convert epoch time to the format yyyy-mm-dd hh:mm:ss. In the first column of the "test_file.csv" there is epoch data? Two other colums are just numbers. Then I want to average every 5 rows. I save averaged data in "averaged_test_file.csv". I would like to save the time in desired format instead of "Time_EPOCH" or next to it. My Python code is as follows:

import pandas as pd

# skip rows due to proper averaging (lost first 5 lines)
df = pd.read_csv("test_file.csv", usecols=[0,1,2], skiprows=range(1,5))

df1=df.groupby(df.index // 5).mean()
df1.to_csv("averaged_test_file.csv", header=['Time_EPOCH', 'No', 'ADC1'], index=False)

The CSV data are as follows: 1603883355.156924,1.0,43.0 1603883355.456939,2.0,36.0 1603883355.756704,3.0,33.0

You can reformat your column from epoch time to a datetime format with:

df['date time'] = pd.to_datetime(df['epoch time'], unit='s')

After that you can export this date column with the 'date_format' parameter of the to_csv method.

df.to_csv("averaged_test_file.csv", date_format='%Y-%m-%d %H:%M:%S')

I found my error. My CSV file has no header so then location is needed.

data.iloc[:,0] = pd.to_datetime(data.iloc[:,0], unit='s')

You need to convert the epoch time to date first and save it back to df, here is the code

df["time_str"] = pd.to_datetime(df["Time_EPOCH"], unit='ms').dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

After that, you can save it to csv or json

df.to_csv("your_file_name")

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