我有一个具有以下结构的Spark数据帧.bodyText_token具有标记(处理/单词集).我有一个已定义关键字的嵌套列表
root
|-- id: string (nullable = true)
|-- body: string (nullable = true)
|-- bodyText_token: array (nullable = true)
keyword_list=['union','workers','strike','pay','rally','free','immigration',],
['farmer','plants','fruits','workers'],['outside','field','party','clothes','fashions']]
Run Code Online (Sandbox Code Playgroud)
我需要检查每个关键字列表下有多少令牌,并将结果添加为现有数据帧的新列.例如:如果tokens =["become", "farmer","rally","workers","student"]
结果是 - > [1,2,0]
以下功能按预期工作.
def label_maker_topic(tokens,topic_words):
twt_list = []
for i in range(0, len(topic_words)):
count = 0
#print(topic_words[i])
for tkn in tokens:
if tkn in topic_words[i]:
count += 1
twt_list.append(count)
return twt_list
Run Code Online (Sandbox Code Playgroud)
我在withColumn下使用了udf来访问该函数,但是我收到了一个错误.我认为这是关于将外部列表传递给udf.有没有办法可以将外部列表和datafram列传递给udf并向我的数据帧添加新列?
topicWord = udf(label_maker_topic,StringType())
myDF=myDF.withColumn("topic_word_count",topicWord(myDF.bodyText_token,keyword_list))
Run Code Online (Sandbox Code Playgroud) python user-defined-functions apache-spark apache-spark-sql pyspark
我想绘制一张澳大利亚地图,并将每个城市都表示为一个点.然后突出人口众多的城市(> 1M)
library(sp)
library(maps)
data(canada.cities)
head(canada.cities)
Run Code Online (Sandbox Code Playgroud)
我已经检查了sp包,可以为加拿大和其他一些国家做这件事.但澳大利亚的细节不存在.有没有一种特殊的方法来获取我们喜欢的国家的数据(城市名称,长,拉特,流行)?