Skip to main content
 首页 » 编程设计

python之引用 Pyspark DataFrame 中的列

2024年10月17日33xing901022

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

  ----- 
| 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]