python处理csv格式数据
1.气温数据分析`在这里插入代码片
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = 'sitka_weather_07-2014.csv' #将文件名称存储在filename中
with open(filename) as f: #打开文件 并存在f中
reader =csv.reader(f) #调用csv.reader()函数 创建一个与文件f相关联的阅读器对象
header_row =next(reader)#函数next()返回文件的下一行 即头文件
#for index,column_header in enumerate(header_row): #调用枚举函数enumerate()来获取每个元素的索引及其值
#print(index,column_header)
#从文件中获取最高温和日期
highs=[]
dates=[]
for row in reader:#遍历余下各行
current_date =datetime.strptime(row[0],"%Y-%m-%d") #调用datetime的方法striptime 按照要求的格式打印出日期
dates.append(current_date)
high=int(row[1])
highs.append(high)
#print(highs)
#根据数据绘制图形
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c="red")
#设置图形格式
plt.title("Daily high temperatures,july 2014",fontsize =24)
plt.xlabel(" ",fontsize =16)
fig.autofmt_xdate()#绘制斜的日期标签,以免他们彼此重叠
plt.ylabel("Temperature(F)",fontsize = 16)
plt.tick_params(axis="both",which="major",labelsize = 16)
plt.show()
2.最高温最低温
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = 'sitka_weather_2014.csv' #将文件名称存储在filename中
with open(filename) as f: #打开文件 并存在f中
reader =csv.reader(f) #调用csv.reader()函数 创建一个与文件f相关联的阅读器对象
header_row =next(reader)#函数next()返回文件的下一行 即头文件
#for index,column_header in enumerate(header_row): #调用枚举函数enumerate()来获取每个元素的索引及其值
#print(index,column_header)
#从文件中获取最高温最低温和日期
highs=[]
dates=[]
lows=[]
for row in reader:#遍历余下各行
current_date =datetime.strptime(row[0],"%Y-%m-%d") #调用datetime的方法striptime 按照要求的格式打印出日期
dates.append(current_date)
high=int(row[1])
highs.append(high)
low =int(row[3])
lows.append(low)
#print(highs)
#根据数据绘制图形 给区域着色
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c="red",alpha =0.5) #alpha 指定颜色的透明度 0表示完全透明 1 表示不透明 默认为1
plt.plot(dates,lows,c="blue",alpha=0.5)
plt.fill_between(dates,highs,lows,facecolor = "blue",alpha = 0.1) #函数fill_between传递一个x两个y 指定填充颜色
#设置图形格式
plt.title("Daily high and low temperatures - 2014",fontsize =24)
plt.xlabel(" ",fontsize =16)
fig.autofmt_xdate()#绘制斜的日期标签,以免他们彼此重叠
plt.ylabel("Temperature(F)",fontsize = 16)
plt.tick_params(axis="both",which="major",labelsize = 16)
plt.show()
3.处理异常数据
import csv
from matplotlib import pyplot as plt
from datetime import datetime
filename = 'death_valley_2014.csv' #将文件名称存储在filename中
with open(filename) as f: #打开文件 并存在f中
reader =csv.reader(f) #调用csv.reader()函数 创建一个与文件f相关联的阅读器对象
header_row =next(reader)#函数next()返回文件的下一行 即头文件
#for index,column_header in enumerate(header_row): #调用枚举函数enumerate()来获取每个元素的索引及其值
#print(index,column_header)
#从文件中获取最高温最低温和日期
highs=[]
dates=[]
lows=[]
for row in reader:#遍历余下各行
try:
current_date = datetime.strptime(row[0], "%Y-%m-%d") # 调用datetime的方法striptime 按照要求的格式打印出日期
high = int(row[1])
low = int(row[3])
except ValueError:
print(current_date,"missing date")
else:
dates.append(current_date)
highs.append(high)
lows.append(low)
#print(highs)
#根据数据绘制图形 给区域着色
fig = plt.figure(dpi=128,figsize=(10,6))
plt.plot(dates,highs,c="red",alpha =0.5) #alpha 指定颜色的透明度 0表示完全透明 1 表示不透明 默认为1
plt.plot(dates,lows,c="blue",alpha=0.5)
plt.fill_between(dates,highs,lows,facecolor = "blue",alpha = 0.1) #函数fill_between传递一个x两个y 指定填充颜色
#设置图形格式
plt.title("Daily high and low temperatures - 2014\nDeath_Valley, CA",fontsize =24)
plt.xlabel(" ",fontsize =20)
fig.autofmt_xdate()#绘制斜的日期标签,以免他们彼此重叠
plt.ylabel("Temperature(F)",fontsize = 16)
plt.tick_params(axis="both",which="major",labelsize = 16)
plt.show()
2014-02-16 00:00:00 missing date