Python使⽤pandas库读取csv⽂件,并分组统计的⼀个例⼦代码:
# coding=gbk
# 从HostWrites.csv读取数据并分组统计
import pandas
import datetime
print"\r\n从 HostWrites.csv 读取数据并分组统计\r\n"
excel = ad_csv("D:\Program Files\CrystalDiskInfo8_2_0\Smart\KXG6AZNV512G TOSHIBA39HS1002TMFQ\HostWrites.csv", header=None)
dates = []
kv = {}
# for row in excel.iterrows():
for row in excel.tail(100).values:
time = _datetime(row[0])
value = row[1]
date = datetime.datetime.strftime(time, "%Y-%m-%d")
if date not in kv:
dates.append(date)
kv[date] = value
last = None
delta = 0
lst = []
for date in dates:
if last is not None:
delta = kv[date] - last
last = kv[date]
today = datetime.datetime.strftime(w(), "%Y-%m-%d")
yesterday = datetime.datetime.strftime(w() + datetime.timedelta(-1), "%Y-%m-%d")
if delta > 50 or date == today or date == yesterday:
lst.append((date, kv[date], delta))
print"⽇期".ljust(12, ""), "固态硬盘写⼊量".ljust(16, ""), "增量"
index = 0
for item in lst:
index += 1
if len(lst) - index < 20:
python怎么读csv数据print str(item[0]).ljust(13, ""), str(item[1]).ljust(15, ""), item[2]
raw_input("输⼊任意字符退出:")
View Code
输出:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。