Python 将Unix时间戳转化为通用时间(年,天,时,分)。处理Dataframe中的Unix时间戳

import time


def time_transform(unix_time):
    tt = {}  # 利用字典存储
    t = time.gmtime(unix_time)  # 重要的是这个函数,可获得更多的信息。这里只取其中4个
    day = t.tm_mday
    hour = t.tm_hour
    min = t.tm_min  # 这里min有歧义,最好利用其他词代表分钟
    sec = t.tm_sec

    tt['day'] = day
    tt['hour'] = hour
    tt['min'] = min
    tt['sec'] = sec
    return tt


# 对dataframe中的unix时间处理
def split_time(tim):
    day = []
    hour = []
    min = []
    sec = []
    for i in range(len(tim)):
        day.append(tim[i]['day'])
        hour.append(tim[i]['hour'])
        min.append(tim[i]['min'])
        sec.append(tim[i]['sec'])
    day = pd.Series(day)
    hour = pd.Series(hour)
    min = pd.Series(min)
    sec = pd.Series(sec)
    return day, hour, min, sec


tim = df['unix_time'].apply(time_transform)
day, hour, min, sec = split_time(tim)
df['day'] = day
df['hour'] = hour
df['min'] = min
df['sec'] = sec
展开阅读全文

没有更多推荐了,返回首页