Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧的多维布尔索引-删除NaN行和列_Python_Numpy_Pandas - Fatal编程技术网

Python 数据帧的多维布尔索引-删除NaN行和列

Python 数据帧的多维布尔索引-删除NaN行和列,python,numpy,pandas,Python,Numpy,Pandas,我有一个像熊猫一样的数据框 df = pd.DataFrame([[1,-2,-3],[4,5,6],[1,3,4]]) 看起来像 0 1 2 0 1 -2 -3 1 4 5 6 2 1 3 4 我想得到这个数据帧的一个子集,它只有负值 1 2 0 -2 -3 我想尝试布尔索引(但我不知道如何使用二维布尔索引) 使用二维布尔索引时,是否有办法(自动)删除包含NaN的列和行?您可以调用两次,dropna接受一个thresh参数,如果存在n非Na值,

我有一个像熊猫一样的数据框

df = pd.DataFrame([[1,-2,-3],[4,5,6],[1,3,4]])
看起来像

   0  1  2
0  1 -2 -3
1  4  5  6
2  1  3  4
我想得到这个数据帧的一个子集,它只有负值

    1    2
0  -2   -3
我想尝试布尔索引(但我不知道如何使用二维布尔索引)

使用二维布尔索引时,是否有办法(自动)删除包含NaN的列和行?

您可以调用两次,
dropna
接受一个
thresh
参数,如果存在
n
非Na值,则该参数不会删除整个轴,因此以下将先删除行,然后删除列:

In [283]:

df[df<0].dropna(axis=0, thresh=1).dropna(axis=1)
Out[283]:
   1  2
0 -2 -3
更新

axis
param接受多个参数,因此实际上您只需一次调用即可完成,谢谢@scls:

In [285]:

df[df<0].dropna(axis=[0,1], thresh=1)
Out[285]:
   1  2
0 -2 -3
[285]中的


df[dfthanks.question editedThanks我不知道
thresh
参数。
df[df>1]。dropna(axis=[0,1],thresh=1)
也可以正常工作
In [283]:

df[df<0].dropna(axis=0, thresh=1).dropna(axis=1)
Out[283]:
   1  2
0 -2 -3
In [284]:

df[df<0].dropna(axis=0, thresh=1)
Out[284]:
    0  1  2
0 NaN -2 -3
In [285]:

df[df<0].dropna(axis=[0,1], thresh=1)
Out[285]:
   1  2
0 -2 -3