python - 引用 Pyspark DataFrame 中的列

标签 python apache-spark pyspark apache-spark-sql

假设我有一个转换为数据框的单词列表

  -----
| word |
  -----
| cat  |
| bird |
| dog  |
| ...  |
  -----

我尝试计算字母数:

from pyspark.sql.functions import length

letter_count_df = words_df.select(length(words_df.word))

我知道这会导致仅包含单列的数据框。

如何在不使用alias的情况下引用letter_count_df的唯一列?

  -------------
| length(word) |
  -------------
|           3  |
|           4  |
|           3  |
|         ...  |
  -------------

最佳答案

姓名:

>>> letter_count_df.select(c)
DataFrame[length(word): int]

或列和名称:

>>> from pyspark.sql.functions import *
>>> letter_count_df.select(c))

其中 c 为常量:

>>> c = "length(word)"

>>> c = letter_count_df.columns[0]

关于python - 引用 Pyspark DataFrame 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097011/

相关文章:

python - 使用 python 通过 databricks 获取 Azure Gen1 中文件的最后修改日期

python - 导入 numpy c 扩展失败

python - Pytest:更新测试中的全局变量

apache-spark - 如何在 Spark 中检索 DataFrame 的别名

PySpark MLLIB 随机森林 : prediction always 0

apache-spark - PySpark:带有标量 Pandas UDF 的无效返回类型

c# - 哪些 Python 特性会激发 C# 开发人员的兴趣?

apache-spark - Spark 笛卡尔不会引起洗牌?

python - 使用 groupby 或 aggregate 合并 RDD 或 DataFrame 中每个事务中的项目来做 FP-growth

python - Pyspark:将不同表中的列相乘