# 数据预处理

4 Likes

2.2.2 处理缺失值一节中，<< 由于 “巷子”（“Alley”）列只接受两种类型的类别值 “Alley” 和 “NaN” >> 应该改为 << 由于 “巷子”（“Alley”）列只接受两种类型的类别值 “Pave” 和 “NaN”>>

3 Likes

Thanks @zppet, 我们已经在这里修复。欢迎下次 contribute 到PR!

1 Like

def drop_col():
n = len(data)
i = 0
for col_name in(‘NumRooms’,‘Alley’,‘Price’):
cnt = data[col_name].count()
if (n-cnt) > i :
i = (n-cnt)
j = col_name
data.drop(j,axis=1,inplace=True)

drop_col()
data

1 Like

def drop_col(m):
num = m.isna().sum() #获得缺失值统计信息
num_dict = num.to_dict() #转为字典
max_key =max(num_dict,key=num_dict.get) #取字典中最大值的键
del m[max_key] #删除缺失值最多的列
return m

drop_col(data)

3 Likes

with open(data_file1, ‘w’)as f: #open文件名参数不要打引号
f.write(‘动物,年龄,特点\n’)
f.write(‘马,4,跑的快\n’)
f.write(‘猪,5,喜欢吃\n’)
f.write(‘羊,NA,NA\n’)
f.write(‘鸡,NA,会下蛋\n’)
f.write(‘牛,3,会吃草\n’)
f.write(‘NA,NA,NA\n’)
data

2 Likes

pb1

number = []
names = [‘NumRooms’,‘Alley’,‘Price’]
for name in names:
a = np.sum(data[name].isnull())
number.append(a)
data = data.drop(names[np.argmax(number)],axis = 1)
data

3 Likes
``````count = 0
count_max = 0
labels = ['NumRooms','Alley','Price']
for label in labels:
count = data[label].isna().sum()
if count > count_max:
count_max = count
flag = label
data_new = data.drop(flag,axis=1)
data_new
``````
18 Likes

1 Like

x=df.isna().sum()
dfs=df.drop(columns=x.index[x.argmax()])

8 Likes
``````names = list(inputs)
max_nan = inputs.isnull().sum().max()
drop_i = []
for n in names:
if inputs[n].isnull().sum() == max_nan:
inputs_drop = inputs.drop(columns=n)

inputs_drop
``````
1 Like

maxnanum=0
for i in inputs.columns:
if maxnanum<inputs[i].isnull().sum():
maxnanum = inputs[i].isnull().sum()
inputs=inputs.dropna(1,‘any’,thresh=inputs.shape[0]-maxnanum+1)

max_nan=[]
for i,row in data.iteritems():
max_nan.append(row.isna().sum())
del data[data.columns[max_nan.index(max(max_nan))]]

df_count = data.isna().sum()
label = df_count.idxmax()
new_data = data.drop(label, 1)

2 Likes

1 Like

# 知道每列的nan数

nan_numer = data1.isnull().sum(axis=0)

# 找到nan_numer(series)中最大数的索引

nan_max_id = nan_numer.idxmax()

# 删除nan最大的列

data1 = data1.drop([nan_max_id], axis=1)
data1

12 Likes

### 小白交作业

• 2021.8.25
``````def drop_max_nan_col(data):
nanmaxid = data.isna().sum(axis = 0).idxmax()
ndata = data
ndata = ndata.drop(nanmaxid, axis = 1)
inputs, output = ndata.iloc[:,0:-1], ndata.iloc[:,-1]
inputs = inputs.fillna(inputs.mean())
inputs = pd.get_dummies(inputs, dummy_na = True)
X, y = torch.tensor(inputs.values), torch.tensor(output.values)
return X, y
``````
1 Like