pandas读取Excel批量转换时间戳怎么实现

工具使用   发布日期:2023年07月19日   浏览次数:547

这篇文章主要介绍“pandas读取Excel批量转换时间戳怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“pandas读取Excel批量转换时间戳怎么实现”文章能帮助大家解决问题。

一、安装

  1. pip install pandas

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

  1. pip install xlrd

二、 代码如下

  1. import pandas as pd
  2. import time,datetime
  3. file_path = r'C:UsersAdministratorDesktop携号转网测试admin_log.xls'
  4. df = pd.read_excel(file_path, sheet_name = "admin",header=None)
  5. # print(df.values[1,0])
  6. data=[]
  7. for i in range(len(df)):
  8. timechuo = df.values[i,0]
  9. time_local = time.localtime(timechuo)
  10. dt = time.strftime("%Y-%m", time_local)
  11. data.append(dt)
  12. result = pd.value_counts(data)
  13. print(result)

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

  1. #将GPS时间戳数据批量转化为日期时间
  2. import numpy as np
  3. import pandas as pd
  4. import time
  5. #设置Spyder右侧console区的print输出行列数无限制
  6. pd.set_option('display.max_columns', None)
  7. pd.set_option('display.max_rows', None)
  8. #读取数据
  9. data=pd.read_excel('C:/Users/Administrator/Desktop/GPS时间.xlsx') #excel文件的路径,命名为GPS时间.xlsx
  10. data.loc[:, 'localminute'] = data['gps_ts'].apply(lambda x :time.localtime(x)) #gps_ts为GPS时间.xlsx的列名,具体见图1
  11. #转换的时间格式为"年-月-日 时:分:秒"
  12. data.loc[:, 'time'] = data['localminute'].apply(lambda x :time.strftime("%Y-%m-%d %H:%M:%S", x))
  13. print(data)

以上就是pandas读取Excel批量转换时间戳怎么实现的详细内容,更多关于pandas读取Excel批量转换时间戳怎么实现的资料请关注九品源码其它相关文章!