Python - 从 Pandas 中移除所有值为 null 的列
要移除所有值为 null 的列,请使用 dropna() 方法并设置“how”参数为“all”−
how='all'
首先,让我们使用它们各自的别名导入所需的库 −
import pandas as pd import numpy as np
创建一个 DataFrame。我们已使用 Numpy np.inf 设置 NaN 值
dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN,np.NAN] } )
要移除所有值为 null 的列,请使用 dropna() 并设置所需的参数 −
dataFrame.dropna(how='all', axis=1, inplace=True)
示例
以下为代码 −
import numpy as np import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN,np.NAN] } ) print"DataFrame ...\n",dataFrame dataFrame.dropna(how='all', axis=1, inplace=True) print"\nDataFrame ...\n",dataFrame
输出
将生成以下输出 −
DataFrame ... Result Student 0 NaN Jack 1 NaN Robin 2 NaN Ted 3 NaN Robin 4 NaN Scarlett 5 NaN Kat 6 NaN Ted DataFrame ... Student 0 Jack 1 Robin 2 Ted 3 Robin 4 Scarlett 5 Kat 6 Ted
广告