我正在使用 kaggle 中的一些数据集。我想获取所有列标签(功能)作为这些功能的列表,其中没有单个缺失值
。我已经做到了(我认为是这样),但我想知道是否有更好的方法来做到这一点。这是我的代码,结果是那些没有单个缺失值的功能的列表:
import matplotlib as plt
data = pd.read_csv(r'C:\Users\.kaggle\house-prices\train.csv')
result = data.isnull().sum(axis=0)[data.isnull().sum(axis=0) ==
0].index.tolist()
例如,如果我运行以下代码:
d = { 'Feature 1': [None, 1, 2, None ], 'Feature 2': [4, 5, 5, 6],
'Feature 3': [7, 7, 8, 9 ]}
df = pd.DataFrame(data = d)
print(df
print(df.isnull().sum(axis=0)[df.isnull().sum(axis=0) ==
0].index.tolist())
我将得到以下结果:
Feature 1 Feature 2 Feature 3
0 NaN 4 7
1 1.0 5 7
2 2.0 5 8
3 NaN 6 9
['Feature 2', 'Feature 3']
最佳答案
使用dropna
并将列名称转换为列表
:
print (df.dropna(axis=1).columns.tolist())
['Feature 2', 'Feature 3']
详细信息:
print (df.dropna(axis=1))
Feature 2 Feature 3
0 4 7
1 5 7
2 5 8
3 6 9
关于python-3.x - 有没有更好的方法来获取标签作为数据框中没有单个缺失值的那些特征(列)的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085218/