dataframe - 在 DataFrame 上描述 vs printSchema 方法

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

describe() 将分数列作为字符串给出,而当我不使用括号进行描述或使用 printSchema() 时,它会将分数列作为 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)

最佳答案

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

/**
   * 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.
   *

https://stackoverflow.com/questions/61343134/

相关文章:

python - tf.print 何时会按预期实际工作(即打印张量和变量的值)?

linux - 如何将频率缩放驱动程序从 "intel-pstate"更改为 "acpi-cpufr

reactjs - 不变失败 : You should not use outside

c# - 了解 C# 中的非托管关键字

c# - 如何使用预定义标记列表实现解析器/解释器?

python - 写一个函数来乘以复数

react-native - 使用 create-react-native-app 创建新项目时出现

php - 从 laravel 中的数据库动态填充语言翻译数组

reactjs - 如何将 firebase 与 React 功能组件同步?

tensorflow - 如何将 Tensorflow model.json 转换为 model.p