用fancyimpute和pandas进行数据归类
我以大熊猫数据着称df
。它有很多缺失。不能逐行或逐行删除。插补中位数,均值或最频繁的值也不是一种选择(因此插补pandas
和/或scikit
不幸的是,不能做到这一点)。
我碰到了一个看起来很整洁的程序包fancyimpute
(您可以在这里找到它)。但是我有一些问题。
这是我的工作:
#the neccesary imports
import pandas as pd
import numpy as np
from fancyimpute import KNN
# df is my data frame with the missings. I keep only floats
df_numeric = = df.select_dtypes(include=[np.float])
# I now run fancyimpute KNN,
# it returns a np.array which I store as a pandas dataframe
df_filled = pd.DataFrame(KNN(3).complete(df_numeric))
但是,df_filled
某种程度上是单个矢量,而不是填充的数据帧。如何获得带有插补的数据框?
更新资料
我意识到,fancyimpute
需要一个numpay array
。因此,我使用将转换df_numeric
为一个数组as_matrix()
。
# df is my data frame with the missings. I keep only floats
df_numeric = df.select_dtypes(include=[np.float]).as_matrix()
# I now run fancyimpute KNN,
# it returns a np.array which I store as a pandas dataframe
df_filled = pd.DataFrame(KNN(3).complete(df_numeric))
输出是缺少列标签的数据框。有没有办法找回标签?