python
# -*- coding: utf-8 -*-"""Created on Fri Jan 19 19:00:01 2018@author: Allen"""import pandas as pdfrom scipy.interpolate import lagrangeinputfile = "catering_sale.xls"outputfile = "sales.xls"data = pd.read_excel(inputfile)data[u"銷量"][(data[u"銷量"]<400) | (data[u"銷量"]>5000)] = None#自定義列向量插值函數#s為列向量,n為被插值的位置,k為取前後的數據個數,默認為5def ployinterp_column(s, n, k=5): y = s[list(range(n-k, n)) + list(range(n+1, n+1+k))]# print("第%d個數空缺" %n)# print(list(range(n-k,n)))# print(list(range(n-k, n)) + list(range(n+1, n+1+k)))# print(s[list(range(n-k, n)) + list(range(n+1, n+1+k))]) #print(list(range(n+1, n+1+k))) y = y[y.notnull()] #剔除空值 return lagrange(y.index, list(y))(n) #插值並返回插值結果#逐個元素判斷是否需要插值for i in data.columns: for j in range(len(data)): if (data[i].isnull())[j]: #如果為空即插值 data[i][j] = ployinterp_column(data[i], j) print(data[i][j])data.to_excel(outputfile) #輸出結果,寫入文件
list為空缺值附近的十個序列值
y.index為10個值的索引值
y.notnull()返回的是True
////////////////////////////////////////////////////////////////////////////////////////////////
# -*- coding: utf-8 -*-"""Created on Fri Jan 19 16:11:59 2018@author: Allen"""import numpy as npimport matplotlib.pyplot as pltimport pandas as pdx = np.linspace(0, 2*np.pi, 50)y = np.sin(x)plt.plot(x, y, "bp--")plt.show()labels = "Frogs", "Hogs", "Dogs", "Logs"sizes = [15, 30, 5, 50]colors = ["yellowgreen", "gold", "lightskyblue", "lightcoral"]explode = (0, 0.3, 0, 0)#突出顯示plt.pie(sizes, explode = explode, labels = labels, colors=colors, autopct = "%1.1f%%" ,shadow = True, startangle = 90)plt.axis("equal")plt.show()s = np.random.randn(1000)plt.hist(s, 10)D = pd.DataFrame([s, s+3]).T#構造兩列的DataFrameD.plot(kind = "box")plt.show()plt.rcParams["font.sans-serif"] = ["SimHei"]plt.rcParams["axes.unicode_minus"] = Falsex1 = pd.Series(np.exp(np.arange(20)))x1.plot(label = u"原始數據", legend = True)plt.show()x1.plot(logy = True, label = u"對數數據圖", legend = True)plt.show()erro = np.random.randn(10)y = pd.Series(np.sin(np.arange(10)))y.plot(yerr = erro)plt.show()
推薦閱讀:
※記一次調試python內存泄露的問題
※【小林的OpenCV基礎課 2】Hello World!
※數據分析修鍊歷程:你在哪一站?
※[E0] Python入門
※你什麼時候對 Python 感到絕望?
TAG:Python |