如果满足 NaN 阈值,Python 将删除 DF 中的所有特征实例

标签 python pandas dataframe nan

使用df.dropna(thresh = x, inplace=True) ,我可以成功删除至少缺少 x 的行非纳米值。

但是因为我的 df 看起来像:

          2001     2002     2003    2004

bob   A   123      31       4        12
bob   B   41        1       56       13
bob   C   nan      nan      4        nan

bill  A   451      8        nan      24
bill  B   32       5        52        6
bill  C   623      12       41       14

#Repeating features (A,B,C) for each index/name

这会删除 thresh= 所在的一行/实例满足条件,但保留该功能的其他实例。

What I want is something that drops the entire feature, if the thresh is met for any one row, such as:

df.dropna(thresh = 2, inplace=True):

           2001     2002     2003    2004

bob    A    123      31       4        12
bob    B    41        1       56       13

bill   A    451      8        nan      24
bill   B    32       5        52        6

#Drops C from the whole df

其中 C被从整个 df 中删除,而不仅仅是满足 bob 下的条件的一次

最佳答案

您的示例看起来像一个多索引索引数据帧,其中索引级别 1 是功能 A、B、C,索引级别 0 是名称。您可以使用 notnasum 创建掩码来识别非 nan 值数量小于 2 的行并获取其索引级别 1 值。最后,使用 df.query 来切片行

a = df.notna().sum(1).lt(2).loc[lambda x: x].index.get_level_values(1)
df_final = df.query('ilevel_1 not in @a')

Out[275]:
         2001  2002  2003  2004
bob  A  123.0  31.0   4.0  12.0
     B   41.0   1.0  56.0  13.0
bill A  451.0   8.0   NaN  24.0
     B   32.0   5.0  52.0   6.0

方法2:
使用 notnasumgroupbytransform 在具有以下条件的组上创建掩码 True大于或等于 2 的非 nan 值。最后,使用此掩码对行进行切片

m = df.notna().sum(1).groupby(level=1).transform(lambda x: x.ge(2).all())
df_final = df[m]

Out[296]:
         2001  2002  2003  2004
bob  A  123.0  31.0   4.0  12.0
     B   41.0   1.0  56.0  13.0
bill A  451.0   8.0   NaN  24.0
     B   32.0   5.0  52.0   6.0

关于如果满足 NaN 阈值,Python 将删除 DF 中的所有特征实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59593901/

相关文章:

scala - 如何将 RDD[Row] 转换回 DataFrame

python - 如何在 python PyOpenGL 中旋转魔方的切片?

python - 使用命令行参数从另一个 Python 脚本打开 IDLE

python - 只读取大型 JSON 中的特定字段并导入 Pandas Dataframe

python - 如何快速轻松地按 pandas 数据框中列 [0] 的值选择单行

python - 选择 pandas df 中的字符串列(相当于 df.select_dtypes)

python - 在 IPython 中自动回显赋值语句的结果

python - 是否可以在 Flask-WTForms 中设置标签的一部分样式?

python - 按列迭代 csv

python - groupby 和过滤 Pandas