【问题标题】:describe vs printSchema methods on DataFrame在 DataFrame 上描述 vs printSchema 方法
【发布时间】:2020-08-04 04:13:11
【问题描述】:

我在 pyspark 中运行此代码,并且 describe 和 printSchema 之间的输出差异令人困惑。请看下面的代码。

describe() 将 score 列作为字符串给出,而当我不带括号进行描述或使用 printSchema() 时,它将 score 列作为 int 给出 - 它实际上是。

这是我的数据框。

>>> df.show()
+-------+------+-----+
|   name|course|score|
+-------+------+-----+
| fsdhfu|     a|   56|
| sdjjfd|     a|   57|
|kljsjlk|     b|   23|
|  udjkx|     b|   89|
|    ias|     c|   36|
| jksdkj|     c|   37|
|  usdkj|     d|   48|
+-------+------+-----+

使用描述:

>>> df2.describe()
DataFrame[summary: string, name: string, course: string, score: string]
>>> df2.describe
<bound method DataFrame.describe of DataFrame[name: string, course: string, score: int]>

使用 printSchema:

>>> df2.printSchema()
root
 |-- name: string (nullable = true)
 |-- course: string (nullable = true)
 |-- score: integer (nullable = true)

【问题讨论】:

    标签: dataframe apache-spark pyspark schema describe


    【解决方案1】:

    它们之间的区别在于 schema 为您提供有关列的信息,例如列的名称及其数据类型,而 describe 提供有关数据集的统计信息。以下是关于描述的火花文档:

    /**
       * Computes basic statistics for numeric and string columns, including count, mean, stddev, min,
       * and max. If no columns are given, this function computes statistics for all numerical or
       * string columns.
       *
       * This function is meant for exploratory data analysis, as we make no guarantee about the
       * backward compatibility of the schema of the resulting Dataset. If you want to
       * programmatically compute summary statistics, use the `agg` function instead.
       *
       * {{{
       *   ds.describe("age", "height").show()
       *
       *   // output:
       *   // summary age   height
       *   // count   10.0  10.0
       *   // mean    53.3  178.05
       *   // stddev  11.6  15.7
       *   // min     18.0  163.0
       *   // max     92.0  192.0
       * }}}
       *
       * Use [[summary]] for expanded statistics and control over which statistics to compute.
       *
    

    【讨论】:

    • 嘿,谢谢。实际上,我错过了实际的问题,即“describe() 将 score 列作为字符串给出,而当我不带括号进行描述或使用 printSchema() 时,它会将 score 列作为 int - 它实际上是。”。我已经更新了。
    【解决方案2】:

    在 python 中。 foo.bar 和 foo.bar() 都是有效的语句,其中 foo 是一个对象,而 bar 是在代表对象 foo 的类中定义的方法。在前一种情况下,您访问了绑定到 foo 对象的方法,但您没有调用该方法。

    现在来到 pyspark。声明 df2.describe 告诉我们它在 df2 数据帧上找到了一个名为 describe 的方法,这是正确的。当您调用 df2.describe() 中的方法 describe 时,您会得到一个新的数据帧。您必须在结果上调用 show 方法以获取与原始数据帧相关联的统计信息。我建议连续运行以下 3 个命令。

    df2.describe
    df2.describe()
    df2.describe().show()
    

    【讨论】:

      【解决方案3】:

      describe() 不是数据集的模式,它包含数据集的统计信息(max、min、mean、stedv、..)。它甚至还有一个称为摘要的附加列。 SparkSQL 将这些数据以字符串格式存储为 SQPRK SQL 数据帧。 printSchema() 反映了列的类型。所以这两者是互补的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-10
        • 2014-09-11
        • 1970-01-01
        • 2017-05-27
        • 2017-04-02
        • 1970-01-01
        相关资源
        最近更新 更多