python處理csv格式資料

2021-09-10 06:29:58 字數 3995 閱讀 9328

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中

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 按照要求的格式列印出日期

high=int(row[1])

#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()#繪製斜的日期標籤,以免他們彼此重疊

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中

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 按照要求的格式列印出日期

high=int(row[1])

low =int(row[3])

#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()#繪製斜的日期標籤,以免他們彼此重疊

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中

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:

#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()#繪製斜的日期標籤,以免他們彼此重疊

python 處理csv資料

python中有專門的模組csv來處理 例如 csv檔案的格式 263 invalid creation wissam a.elriachy 2010 03 27 02 58 45 2010 04 22 22 41 40 例如讀取csv的資料列印出來 import csv reader csv.re...

pandas處理csv格式的資料

import pandas as pd import numpy as np from pylab import mpl.rcparams font.sans serif simhei mpl.rcparams axes.unicode minus false file path d 演算法比賽 件...

用python處理csv格式檔案

在各種平台上獲取資料時,我們常常獲得的是csv格式的檔案。csv格式是一種逗號分隔值的檔案格式,它並不是非常reader friendly。所幸,python標準庫中的csv模組可以幫助我們輕鬆處理csv格式檔案。下面將以分析我國2010 2019年gdp為例簡單介紹用python處理csv格式檔案...