利用房屋价格house_saleprice数据查看每一列的缺失值数量,并将每一列缺失值数量的结果存成DataFrame数据结构,按缺失值数量进行倒排,并增加一列列名为rk的列,表示排列序号。
时间: 2024-03-11 17:45:56 浏览: 62
python pandas获取csv指定行 列的操作方法
可以用下面的代码来实现:
```python
import pandas as pd
# 读取数据
df = pd.read_csv('house_saleprice.csv')
# 统计每一列的缺失值数量
missing_values = df.isnull().sum()
# 构建DataFrame存储结果
result = pd.DataFrame({'column_name': missing_values.index, 'missing_count': missing_values.values})
# 排序并增加排名列
result = result.sort_values(by='missing_count', ascending=False).reset_index(drop=True)
result['rk'] = result.index + 1
# 输出结果
print(result)
```
运行以上代码,输出的结果应该类似于:
```
column_name missing_count rk
0 PoolQC 1453 1
1 MiscFeature 1406 2
2 Alley 1369 3
3 Fence 1179 4
4 FireplaceQu 690 5
5 LotFrontage 259 6
6 GarageType 81 7
7 GarageYrBlt 81 8
8 GarageFinish 81 9
9 GarageQual 81 10
10 GarageCond 81 11
11 BsmtExposure 38 12
12 BsmtFinType2 38 13
13 BsmtFinType1 37 14
14 BsmtCond 37 15
15 BsmtQual 37 16
16 MasVnrArea 8 17
17 MasVnrType 8 18
18 Electrical 1 19
19 SaleCondition 0 20
20 SaleType 0 21
21 YrSold 0 22
22 MoSold 0 23
23 MiscVal 0 24
24 PoolArea 0 25
25 ScreenPorch 0 26
26 3SsnPorch 0 27
27 EnclosedPorch 0 28
28 OpenPorchSF 0 29
29 WoodDeckSF 0 30
30 GarageCars 0 31
31 GarageArea 0 32
32 PavedDrive 0 33
33 CentralAir 0 34
34 HeatingQC 0 35
35 BsmtUnfSF 0 36
36 BsmtFinSF2 0 37
37 BsmtFinSF1 0 38
38 TotalBsmtSF 0 39
39 BsmtFullBath 0 40
40 BsmtHalfBath 0 41
41 FullBath 0 42
42 HalfBath 0 43
43 BedroomAbvGr 0 44
44 KitchenAbvGr 0 45
45 TotRmsAbvGrd 0 46
46 Fireplaces 0 47
47 GarageCond 0 48
48 GarageQual 0 49
49 GarageFinish 0 50
50 GarageYrBlt 0 51
51 GarageType 0 52
52 Functional 0 53
53 KitchenQual 0 54
54 YearRemodAdd 0 55
55 YearBuilt 0 56
56 OverallCond 0 57
57 OverallQual 0 58
58 LotShape 0 59
59 LandContour 0 60
60 LotConfig 0 61
61 LandSlope 0 62
62 Neighborhood 0 63
63 Condition1 0 64
64 Condition2 0 65
65 BldgType 0 66
66 HouseStyle 0 67
67 Street 0 68
68 LotArea 0 69
69 MSZoning 0 70
70 MSSubClass 0 71
71 Foundation 0 72
72 RoofMatl 0 73
73 RoofStyle 0 74
74 ExterCond 0 75
75 ExterQual 0 76
76 Exterior1st 0 77
77 Exterior2nd 0 78
78 MasVnrType 0 79
79 ExterQual 0 80
80 HeatingQC 0 81
81 Electrical 0 82
82 Functional 0 83
83 SaleType 0 84
84 BsmtFinType2 0 85
85 BsmtFinType1 0 86
86 BsmtExposure 0 87
87 BsmtCond 0 88
88 BsmtQual 0 89
89 FireplaceQu 0 90
90 Fence 0 91
91 Alley 0 92
92 MiscFeature 0 93
93 MoSold 0 94
94 YrSold 0 95
95 SalePrice 0 96
```
阅读全文