完成李沐书上的练习
下面展示一些 pandas删除最多缺失值的列。
// 方案一 NumRooms Alley Price 0 NaN Pave 127500 1 2.0 NaN 106000 2 4.0 NaN 178100 3 NaN NaN 140000
data.isnull() #判断缺乏值,有则为True NumRooms Alley Price 0 True False False 1 False True False 2 False True False 3 True True False
data.isnull().sum() #计算列中缺乏值得总数 NumRooms 2 Alley 3 Price 0 dtype: int64
data.isnull().sum().idxmax() #得到最大缺失值的索引 'Alley'
data.drop(data.isnull().sum().idxmax(),axis=1) #按列删除 ‘Alley’ NumRooms Price 0 NaN 127500 1 2.0 106000 2 4.0 178100 3 NaN 140000
方案二 data.count(axis='index') #直接按列得到非缺失值的个数, NumRooms 2 Alley 1 Price 4 dtype: int64 data.drop(data.count(axis='index').idxmin(),axis=1) NumRooms Price 0 NaN 127500 1 2.0 106000 2 4.0 178100 3 NaN 140000
data.drop(data.count(axis='index').idxmin(),axis=1,inplace=True) #替换原来的data data NumRooms Price 0 NaN 127500 1 2.0 106000 2 4.0 178100 3 NaN 140000



