利用python处理时间戳

利用python处理时间戳_第1张图片

 

拿这个表(此表为2018年深圳杯D题源数据)举例子,'a'这一列是时间戳,为了数据处理的方便,把时间戳给分成年,月,日,时,秒。
代码实现如下:

 
 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Alpha Labs


if __name__ == '__main__':
    from datetime import datetime
    import pandas as pd
    import numpy as np
    import time

    path = 'time_un_processed.csv'
    df = pd.read_csv(path)
    a = df['a']   # a 这一列是时间戳
    b = [time.gmtime(i) for i in a]  #time.gmtime() 接收时间戳(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t。注:t.tm_isdst始终为0
    Y = [] 
    M = []
    D = []
    H = []
    MIN = []
    S = []
    for i in b:
        a_1, a_2, a_3, a_4, a_5, a_6, a_7, a_8, a_9 = i
        Y.append(a_1)
        M.append(a_2)
        D.append(a_3)
        H.append(a_4)
        MIN.append(a_5)
        S.append(a_6)

    S_Y = pd.Series(Y, name='Year')
    S_M = pd.Series(M, name='Month')
    S_D = pd.Series(D, name='Day')
    S_H = pd.Series(H, name='Hour')
    S_MIN = pd.Series(MIN, name='Minute')
    S_S = pd.Series(S, name='s')

    time_df = pd.concat([df, S_Y, S_M, S_D, S_H, S_MIN, S_S], axis=1)
    print(time_df.head())
    path_2 = './time_processed.csv'

    time_df.to_csv(path_2)


转化后的csv表格为:    利用python处理时间戳_第2张图片

你可能感兴趣的:(Python,Python)