【问题标题】:how to use .iloc with .isnull()如何将 .iloc 与 .isnull() 一起使用
【发布时间】:2018-10-14 13:09:50
【问题描述】:

我被要求使用 .isnull () 函数来验证 DataFrame 上的缺失值。然后在不使用 .head () 函数的情况下打印前 10 行数据。您可以使用 .loc () 或 iloc() 函数。不要将选定的数据存储到任何 DataFrame 中,您只想查看。查看数据的前 10 行。

我编码为:

biz.isnull().iloc[0,10]                      

但它显示 IndexError:

单个位置索引器超出范围

我应该编写什么代码来输出实现了 .isnull() 的数据帧?

【问题讨论】:

  • biz[biz.isnull()].iloc[0,10]

标签: python python-3.x


【解决方案1】:

首先,应该以biz.iloc[:10]biz.iloc[0:10] 的形式检索前10 行,而不是用逗号分隔。

biz.isnull().iloc[:10] 会告诉您 biz 前 10 行中的每个元素是否为空。 biz[biz.isnull()] 接受所有为空的内容,因此仅返回空矩阵。 any(biz.isnull()) 验证矩阵的完整性。
biz[biz.isnull()] = -1 为 df 中的每个空值分配一个默认值。

【讨论】:

    【解决方案2】:

    首先,您应该了解数据框的索引。 在你的情况下:

    biz.iloc[:10]
    

    这将打印数据框的前 10 行,请参阅

    dataframe.iloc[: , :]
    
                 /       \
    this is for rows       this is for columns
    

    现在如果你想计算每列有多少缺失值,你可以使用

    dataframe.isnull().sum(axis=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多