python如何处理缺失数据
How to deal with missing data by python
我有一组数据存储在txt文件中如下:
Elevation(ROAD) Interval
1.3 1
3.3 2
4.1 3
-1.5 4
NA 5
NA 6
6.8 7
2.1 8
5.1 9
NA 10
6.1 11
NA 12
NA 13
NA 14
有什么方法可以使用 python 来插入这些缺失数据 (NA)?例如使用平均技术
你没有提供太多细节。你也不显示代码。
获得所需内容的一种简单方法是创建一个 pandas.Series()
并对其应用 interpolate
函数(如果需要特定的插值设置,则为它应用 google;它们可能根据您使用的 pandas
版本略有不同。
(我的理解是您的 Interval 列是一个简单的数据帧索引)。
import pandas as pd
import numpy as np
data = [1.3, 3.3, 4.1 -1.5, np.nan , np.nan , 6.8, 2.1, 5.1, np.nan, 6.1, np.nan , np.nan , np.nan]
ser = pd.Series(data)
ser.interpolate()
假设您的 pandas
数据框为 df
df['Elevation'].fillna((df['Elevation'].mean()), inplace=True)
试试这个!
如果无论如何都不能使用外部库:
file_content = """1.3
3.3
4.1
-1.5
NA
NA
6.8
2.1
5.1
NA
6.1
NA
NA
NA
7.1
NA"""
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
class ParsedList:
def __init__(self):
self.list = []
self.holes = {} # index key, value length
def set_value(self, number):
if isfloat(number):
self.list.append(float(number))
else:
key = len(self.list)-1
if key in self.holes:
self.holes[key] += 1
else:
self.holes[key] = 1
def interpolate(self):
output = list(self.list)
offset=0
for index, size in self.holes.items():
if index < len(self.list)-1:
delta = (self.list[index+1] - self.list[index])/(size+1)
init_value = self.list[index]
else:
delta =0
init_value = self.list[-1]
for i in range(size):
output.insert(index+i+1+offset, init_value+delta*(i+1))
offset+=size
return output
# test:
parsed_list = ParsedList()
for x in file_content.splitlines():
parsed_list.set_value(x)
[print(x) for x in parsed_list.interpolate()]
我有一组数据存储在txt文件中如下:
Elevation(ROAD) Interval
1.3 1
3.3 2
4.1 3
-1.5 4
NA 5
NA 6
6.8 7
2.1 8
5.1 9
NA 10
6.1 11
NA 12
NA 13
NA 14
有什么方法可以使用 python 来插入这些缺失数据 (NA)?例如使用平均技术
你没有提供太多细节。你也不显示代码。
获得所需内容的一种简单方法是创建一个 pandas.Series()
并对其应用 interpolate
函数(如果需要特定的插值设置,则为它应用 google;它们可能根据您使用的 pandas
版本略有不同。
(我的理解是您的 Interval 列是一个简单的数据帧索引)。
import pandas as pd
import numpy as np
data = [1.3, 3.3, 4.1 -1.5, np.nan , np.nan , 6.8, 2.1, 5.1, np.nan, 6.1, np.nan , np.nan , np.nan]
ser = pd.Series(data)
ser.interpolate()
假设您的 pandas
数据框为 df
df['Elevation'].fillna((df['Elevation'].mean()), inplace=True)
试试这个!
如果无论如何都不能使用外部库:
file_content = """1.3
3.3
4.1
-1.5
NA
NA
6.8
2.1
5.1
NA
6.1
NA
NA
NA
7.1
NA"""
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
class ParsedList:
def __init__(self):
self.list = []
self.holes = {} # index key, value length
def set_value(self, number):
if isfloat(number):
self.list.append(float(number))
else:
key = len(self.list)-1
if key in self.holes:
self.holes[key] += 1
else:
self.holes[key] = 1
def interpolate(self):
output = list(self.list)
offset=0
for index, size in self.holes.items():
if index < len(self.list)-1:
delta = (self.list[index+1] - self.list[index])/(size+1)
init_value = self.list[index]
else:
delta =0
init_value = self.list[-1]
for i in range(size):
output.insert(index+i+1+offset, init_value+delta*(i+1))
offset+=size
return output
# test:
parsed_list = ParsedList()
for x in file_content.splitlines():
parsed_list.set_value(x)
[print(x) for x in parsed_list.interpolate()]