如果前五列为NaN,则删除行 [英] Remove Row if NaN in First Five Columns

查看:58
本文介绍了如果前五列为NaN,则删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,尺寸为89行x 13列.如果前五列中出现 NaN ,我想删除整个.这是一个例子.

I have a pandas dataframe with dimensions 89 rows by 13 columns. I want to remove an entire row if NaN appears within the first five columns. Here is an example.

LotName     C15   C16  C17  C18  C19 Spots15 Spots16 ...
Cherry St   439   464  555  239  420     101     101 ...
Springhurst NaN   NaN  NaN  NaN  NaN      12      12
Barton Lot   34    24   43   45   39      10       9 ...

在上面的示例中,我想删除 Springhurst 观察值,因为它在前五列中包含 NaN .我将如何在Python中做到这一点?

In the above example, I would want to remove the Springhurst observation, as it contains NaN within the first five columns. How would I be able to do this in Python?

推荐答案

如果要对前5列的所有行都严格检查 Nan

If you want to do a strict check of Nan in all rows for first 5 columns:

df.iloc[:, :5].dropna(how='all')

说明:

df.iloc [:,:5] :选择所有行和前5列

df.iloc[:, :5] : select all rows and first 5 columns

.dropna(how ='all'):检查一行中的所有值是否都是 NaN

.dropna(how='all') : check if all values in a row are NaN

如果要在5列中的任何一列中检查 Nan :

If you want to check for Nan in any of the 5 columns:

df.iloc[:, :5].dropna(how='any')

要将其分配回原始df,您可以执行以下操作:

In [2107]: ix = df.iloc[:, :5].dropna(how='all').index.tolist()

In [2110]: df = df.loc[ix]

In [2111]: df
Out[2111]: 
       LotName    C15    C16    C17    C18  C19  Spots15  Spots16
Cherry      St  439.0  464.0  555.0  239.0  420      101    101.0
Barton     Lot   34.0   24.0   43.0   45.0   39       10      9.0

这篇关于如果前五列为NaN,则删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆