在熊猫数据框中显示具有一个或多个NaN值的行


81

我有一个数据框,其中某些行包含缺失值。

In [31]: df.head()
Out[31]: 
                             alpha1  alpha2    gamma1    gamma2       chi2min  
filename                                                                        
M66_MI_NSRh35d32kpoints.dat  0.8016  0.9283  1.000000  0.074804  3.985599e+01   
F71_sMI_DMRI51d.dat          0.0000  0.0000       NaN  0.000000  1.000000e+25   
F62_sMI_St22d7.dat           1.7210  3.8330  0.237480  0.150000  1.091832e+01   
F41_Car_HOC498d.dat          1.1670  2.8090  0.364190  0.300000  7.966335e+00   
F78_MI_547d.dat              1.8970  5.4590  0.095319  0.100000  2.593468e+01 

我想在屏幕上显示那些行。如果尝试的话df.isnull(),它将使用True和给出较长的数据帧False。有什么方法可以选择这些行并将它们打印在屏幕上?

Answers:


159

您可以使用DataFrame.any带有参数axis=1的至少一个核查True通过行DataFrame.isnaboolean indexing

df1 = df[df.isna().any(axis=1)]

d = {'filename': ['M66_MI_NSRh35d32kpoints.dat', 'F71_sMI_DMRI51d.dat', 'F62_sMI_St22d7.dat', 'F41_Car_HOC498d.dat', 'F78_MI_547d.dat'], 'alpha1': [0.8016, 0.0, 1.721, 1.167, 1.897], 'alpha2': [0.9283, 0.0, 3.833, 2.809, 5.459], 'gamma1': [1.0, np.nan, 0.23748000000000002, 0.36419, 0.095319], 'gamma2': [0.074804, 0.0, 0.15, 0.3, np.nan], 'chi2min': [39.855990000000006, 1e+25, 10.91832, 7.966335000000001, 25.93468]}
df = pd.DataFrame(d).set_index('filename')

print (df)
                             alpha1  alpha2    gamma1    gamma2       chi2min
filename                                                                     
M66_MI_NSRh35d32kpoints.dat  0.8016  0.9283  1.000000  0.074804  3.985599e+01
F71_sMI_DMRI51d.dat          0.0000  0.0000       NaN  0.000000  1.000000e+25
F62_sMI_St22d7.dat           1.7210  3.8330  0.237480  0.150000  1.091832e+01
F41_Car_HOC498d.dat          1.1670  2.8090  0.364190  0.300000  7.966335e+00
F78_MI_547d.dat              1.8970  5.4590  0.095319       NaN  2.593468e+01

说明

print (df.isna())
                            alpha1 alpha2 gamma1 gamma2 chi2min
filename                                                       
M66_MI_NSRh35d32kpoints.dat  False  False  False  False   False
F71_sMI_DMRI51d.dat          False  False   True  False   False
F62_sMI_St22d7.dat           False  False  False  False   False
F41_Car_HOC498d.dat          False  False  False  False   False
F78_MI_547d.dat              False  False  False   True   False

print (df.isna().any(axis=1))
filename
M66_MI_NSRh35d32kpoints.dat    False
F71_sMI_DMRI51d.dat             True
F62_sMI_St22d7.dat             False
F41_Car_HOC498d.dat            False
F78_MI_547d.dat                 True
dtype: bool

df1 = df[df.isna().any(axis=1)]
print (df1)
                     alpha1  alpha2    gamma1  gamma2       chi2min
filename                                                           
F71_sMI_DMRI51d.dat   0.000   0.000       NaN     0.0  1.000000e+25
F78_MI_547d.dat       1.897   5.459  0.095319     NaN  2.593468e+01

2
轴:{0或'index',1或'columns'}或其元组/列表传递元组或列表以在多个轴上放置
nuest

4
假设您只想应用于isnull()某些列?
盖德(Gathide)

2
df [df ['gamma1','gamma2']。isna()。any(axis = 1)]或一列为df [df ['gamma1']。isna()]。无论我们是在整个数据框中还是在几列中检查空值,想法都是相同的。在应用了用于布尔索引的isna()之后,我们得到布尔系列。
Jchenna


0

假设gamma1和gamma2是df.isnull()。any()为其提供True值的两个这样的列,可以使用以下代码来打印行。

bool1 = pd.isnull(df['gamma1'])
bool2 = pd.isnull(df['gamma2'])
df[bool1]
df[bool2]

0

也可以尝试此操作,几乎与以前的答案类似。

    d = {'filename': ['M66_MI_NSRh35d32kpoints.dat', 'F71_sMI_DMRI51d.dat', 'F62_sMI_St22d7.dat', 'F41_Car_HOC498d.dat', 'F78_MI_547d.dat'], 'alpha1': [0.8016, 0.0, 1.721, 1.167, 1.897], 'alpha2': [0.9283, 0.0, 3.833, 2.809, 5.459], 'gamma1': [1.0, np.nan, 0.23748000000000002, 0.36419, 0.095319], 'gamma2': [0.074804, 0.0, 0.15, 0.3, np.nan], 'chi2min': [39.855990000000006, 1e+25, 10.91832, 7.966335000000001, 25.93468]}
    df = pd.DataFrame(d).set_index('filename')

在此处输入图片说明

每列中的空值计数。

df.isnull().sum()

在此处输入图片说明

df.isnull().any(axis=1)

在此处输入图片说明

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.