在数据分析过程中,总会遇到空缺数据。如果显示为折线时,就会有异常波动。
拉格朗日函数就是解决空缺数据的一种方法。
比如数据是这样的。
填充完后,是这样的:
代码:
# encoding: utf-8
"""
@author: 陈年椰子
@contact: [email protected]
@version: 1.0
@file: main.py
@time: 2019/7/3
说明 横向拉格朗日填充
"""
import pandas as pd
from scipy.interpolate import lagrange
import math as ma
def test2():
df = pd.read_excel('data/ldata2.xls')
print('原数据:')
print(df)
df_cols = len(df.columns)
df_rows = len(df)
for i in range(df_rows):
row_data = []
row_index = []
na_cols = []
# 第1列 ID 不计算
for j in range(1, df_cols):
# ma.isnan()判断值是否为空值
if ma.isnan(df.iat[i, j]):
na_cols.append(j)
else:
row_data.append(df.iat[i, j])
row_index.append(j)
f= lagrange(row_index,row_data)
for n in na_cols:
df.iat[i, n] = f(n)
print('填充后:')
print(df)
test2()
扫描二维码关注公众号,回复:
9966422 查看本文章