测试数据帧 pandas 中的行是否为 NULL 值

Mis*_*iss 2 python dataframe pandas

如果在同一行中 id1 和 id2 为空,我必须删除行。我做了这些行但出现错误。

d = {'id1': ['Y22', 'X23', None], 'id2': ['Y10', "Y14", None], 'id3':[77,22,13]}
df = pd.DataFrame(data=d)

df = df[df[pd.notnull(df['id1'])and df[pd.notnull(df['id2'])]]
Run Code Online (Sandbox Code Playgroud)

ank*_*_91 6

使用df.all()onaxis=1并检查id1和中的所有值id2是否不为空,然后将其用作布尔掩码:

df[~df[['id1','id2']].isna().all(axis=1)]
Run Code Online (Sandbox Code Playgroud)
   id1  id2  id3
0  Y22  Y10   77
1  X23  Y14   22
Run Code Online (Sandbox Code Playgroud)