获取列名列表,所有值都是Python中的NaN

时间:2018-05-27 08:38:33

标签: python pandas numpy dataframe

我可以使用Python获取所有值都是NaN的列名列表,返回c和d作为下面数据帧的结果吗?感谢。

df = pd.DataFrame({'a': [1,2,3],'b': [3,4,5], 'c':[np.nan, np.nan, np.nan],
                   'd':[np.nan, np.nan, np.nan]})

   a  b   c   d
0  1  3 NaN NaN
1  2  4 NaN NaN
2  3  5 NaN NaN

2 个答案:

答案 0 :(得分:3)

df.columns使用布尔索引:

res = df.columns[df.isnull().all(0)]

# Index(['c', 'd'], dtype='object')

答案 1 :(得分:1)

@ahbon ,您可以尝试df.any()。请参阅Python交互式终端上执行的以下语句序列。

  

检查http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.any.html

>>> import numpy as np
>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'a':[1,2,3],'b':[3,4,5],'c':[np.nan, np.nan, np.nan],'d':[np.nan, np.nan, np.nan]})
>>> df
   a  b   c   d
0  1  3 NaN NaN
1  2  4 NaN NaN
2  3  5 NaN NaN
>>>
>>> # Remove all columns having all NaN values using DataFrame.any()
...
>>> df_new = df.any()
>>> df_new
a     True
b     True
c    False
d    False
dtype: bool
>>>

最后,

>>> columns = []
>>>
>>> for key, value in df_new.iteritems():
...     if value:
...         columns.append(key)
...
>>> df = pd.DataFrame({'a':[1,2,3],'b':[3,4,5],'c':[np.nan, np.nan, np.nan],'d':[np.nan, np.nan, np.nan]}, columns=columns)
>>>
>>> df
   a  b
0  1  3
1  2  4
2  3  5
>>>