假设我有一个转换为数据框的单词列表
-----
| 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/