使用dropna()选择子集以选择多个列 [英] Selecting a subset using dropna() to select multiple columns

查看:594
本文介绍了使用dropna()选择子集以选择多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下DataFrame:

I have the following DataFrame:

df = pd.DataFrame([[1,2,3,3],[10,20,2,],[10,2,5,],[1,3],[2]],columns = ['a','b','c','d'])

在此DataFrame中,我想删除子集['b','c','d']中所有值均为NA的行,这意味着应该删除最后一行.

From this DataFrame, I want to drop the rows where all values in the subset ['b', 'c', 'd'] are NA, which means the last row should be dropped.

以下代码有效:

df.dropna(subset=['b', 'c', 'd'], how = 'all')

但是,考虑到我将使用更大的数据帧,我想使用范围['b':'d']选择相同的子集.如何选择此子集?

However, considering that I will be working with larger data frames, I would like to select the same subset using the range ['b':'d']. How do I select this subset?

推荐答案

IIUC,使用loc,检索这些列,并将其传递给dropna.

IIUC, use loc, retrieve those columns, and pass that to dropna.

c = df.loc[0, 'b':'d'].columns  # retrieve only the 0th row for efficiency
df = df.dropna(subset=c, how='all')

print(df) 
    a     b    c    d
0   1   2.0  3.0  3.0
1  10  20.0  2.0  NaN
2  10   2.0  5.0  NaN
3   1   3.0  NaN  NaN

这篇关于使用dropna()选择子集以选择多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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