pandas读取Excel批量转换时间戳的实践

2023-02-28 14:02:01 实践 读取 批量转换

一、安装

pip install pandas

如果出报错,不能运行,可以安装

 pip install xlrd

二、 代码如下

import pandas as pd
import time,datetime

file_path = r'C:\Users\Administrator\Desktop\携号转网测试\admin_log.xls'
df = pd.read_excel(file_path, sheet_name = "admin",header=None)
# print(df.values[1,0])
data=[]
for i in range(len(df)):
    timechuo = df.values[i,0]
    time_local = time.localtime(timechuo)
    dt = time.strftime("%Y-%m", time_local)
    data.append(dt)

result = pd.value_counts(data)
print(result)

Python将GPS时间戳批量转换为日期时间(年月日时分秒)

#将GPS时间戳数据批量转化为日期时间
import numpy as np
import pandas as pd
import time

#设置Spyder右侧console区的print输出行列数无限制
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#读取数据
data=pd.read_excel('C:/Users/Administrator/Desktop/GPS时间.xlsx') #excel文件的路径,命名为GPS时间.xlsx
data.loc[:, 'localminute'] = data['gps_ts'].apply(lambda x :time.localtime(x)) #gps_ts为GPS时间.xlsx的列名,具体见图1
#转换的时间格式为"年-月-日 时:分:秒"
data.loc[:, 'time'] = data['localminute'].apply(lambda x :time.strftime("%Y-%m-%d %H:%M:%S", x))
print(data)

到此这篇关于pandas读取Excel批量转换时间戳的实践的文章就介绍到这了,更多相关pandas Excel批量转换时间戳内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章