Python數據分析與挖掘實戰P153
#-*_coding:utf-8 _*_
import pandas as pd
from scipy.interpolate import lagrange
inputfile='G:/學習資料/統計/chapter6/demo/data/missing_data.xls'
outputfile='G:/學習資料/統計/chapter6/demo//tmp/missing_data_processed1.xls'
data=pd.read_excel(inputfile, header=None)
def ployinterp_column(s,n,k=5):
y=s[list(range(n-k,n))+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)
data.to_excel(outputfile,header=None,index=False)
遇到的問題:
1.ImportError: No module named
'xlrd'
在cmd命令行安裝xlrd,代碼:pip install xlrd
2.File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\pandas\core\series.py", line 842
return self.loc[key]
FutureWarning:
Passing list-likes to .loc or [] with any missing label will raise
KeyError in the future, you can use .reindex() as an alternative.
See the documentation here:
https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike
查閱該網址後,將loc[key]改爲reindex(key),問題即可解除。