我的数据框有15列&一些列名是nan,如果列名是nan,如何删除。
列:
['Country', 'Survey', 'a typical day', 'a particularly good day',
'a particularl', 'dk/refused', 'total', nan, nan, nan, nan, nan,
'questionsCode'], dtype=object)
以下列的预期数据框:
['Country', 'Survey', 'a typical day', 'a particularly good day',
'a particularl', 'dk/refused', 'total', questionsCode'], dtype=object)
答案 0 :(得分:2)
<强>设置强>
考虑示例数据框df
cols = [
'Country', 'Survey', 'a typical day',
'a particularly good day', 'a particularl',
'dk/refused', 'total',
np.nan, np.nan, np.nan, np.nan, np.nan,
'questionsCode'
]
df = pd.DataFrame([range(13)], range(2), cols)
df
Country Survey a typical day a particularly good day a particularl dk/refused total NaN NaN NaN NaN NaN questionsCode
0 0 1 2 3 4 5 6 7 8 9 10 11 12
1 0 1 2 3 4 5 6 7 8 9 10 11 12
解决方案1
利用loc
基于标签的索引
df.loc[:, df.columns.dropna()]
Country Survey a typical day a particularly good day a particularl dk/refused total questionsCode
0 0 1 2 3 4 5 6 12
1 0 1 2 3 4 5 6 12
这也会起作用
df[df.columns.dropna()]
解决方案2
使用布尔索引
df.loc[:, df.columns.notnull()]
Country Survey a typical day a particularly good day a particularl dk/refused total questionsCode
0 0 1 2 3 4 5 6 12
1 0 1 2 3 4 5 6 12