使用TabPy的单词云

2024-10-04 01:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在TabPy中创建一些代码,计算列中单词的频率,并删除Tableau中单词云的停止词

在Python中,我可以很容易地做到这一点:

other1_count = other1.answer.str.split(expand=True).stack().value_counts()
other1_count = other1_count.to_frame().reset_index()
other1_count.columns = ['Word', 'Count']

### Remove stopwords
other1_count['Word'] = other1_count['Word'].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))
other1_count['Word'].replace('', np.nan, inplace=True)
other1_count.dropna(subset=['Word'], inplace=True)
other1_count = other1_count[~other1_count.Word.str.contains("nan")]

但不太确定如何通过Tabby运行此功能。有谁熟悉Tabby,知道我怎么跑吗

提前谢谢


Tags: 代码intruecountnan单词word频率
2条回答

我认为熟悉与Tableau相关的Python的最好方法是Tableau社区上的这个(旧)线程:

https://community.tableau.com/s/news/a0A4T000002NznhUAC/tableau-integration-with-python-step-by-step?t=1614700410778

它一步一步地解释了初始设置以及如何通过Tableau计算字段“调用”Python

此外,您将在文章顶部找到对更新更多的Tabby GitHub存储库的引用: https://github.com/tableau/TabPy

我在R工作过一个项目,完成了一些非常类似的事情。这里有一个视频示例,展示了概念验证(没有音频)https://www.screencast.com/t/xa0yemiDPl

它本质上显示了使用Tableau交互检查选定国家的word cloud中葡萄酒描述的最终状态。主要组成部分是:

  • 让Tableau连接到要分析的数据,以及一个占位符数据集,该数据集包含您希望从Python/R代码返回的记录数(Tableau对Python/R的调用期望返回它发送给处理的相同数量的记录……如果您发送文本数据,但处理它以返回更多记录,这可能会有问题,就像word cloud示例中的情况一样)
  • 让Python/R代码连接到您的数据,并在单个向量中返回单词和频率计数,用分隔符分隔(Tableau对单词云需要什么)
  • 使用Tableau计算字段拆分单个向量
  • 利用参数操作选择要传递给Python/R代码的参数值

高层概述overview

表格计算字段-[R字+频率]:

Script_Str('
print("STARTING NEW SCRIPT RUN")
print(Sys.time())
print(.arg2) # grouping
print(.arg1) # selected country


# TEST VARIABLE (non-prod)
.MaxSourceDataRecords = 1000 # -1 to disable

# TABLEAU PARAMETER VARIABLES 
.country = "' + [Country Parameter] + '"
.wordsToReturn = ' + str([Return Top N Words]) + '
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#

# VARIABLES DERIVED FROM TABLEAU PARAMETER VALUES
.countryUseAll = (.country == "All")
print(.countryUseAll)
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#

#setwd("C:/Users/jbelliveau/....FILL IN HERE...")
.fileIn = ' + [Source Data Path] + '
#.fileOut = "winemag-with-DTM.csv"

#install.packages("wordcloud")
#install.packages("RColorBrewer") # not needed if installed wordcloud package

library(tm)
library(wordcloud)
library(RColorBrewer) # color package (maps or wordclouds)

wineAll = read.csv(.fileIn, stringsAsFactors=FALSE)

# TODO separately... polarity 

# use all the data or just the parameter selected
print(.countryUseAll)

if ( .countryUseAll ) {
  wine = wineAll # filter down to parameter passed from Tableau
}else{
  wine = wineAll[c(wineAll$country == .country),] # filter down to parameter passed from Tableau
}

# limited data for speed (NOT FOR PRODUCTION)
if( .MaxSourceDataRecords > 0 ){
  print("limiting the number of records to use from input data")
  wine = head(wine, .MaxSourceDataRecords)  
}


corpus = Corpus(VectorSource(wine$description))
corpus = tm_map(corpus, tolower)
#corpus = tm_map(corpus, PlainTextDocument) # https://stackoverflow.com/questions/32523544/how-to-remove-error-in-term-document-matrix-in-r/36161902
corpus = tm_map(corpus, removePunctuation)
corpus = tm_map(corpus, removeWords, stopwords("English"))
#length(corpus)

dtm = DocumentTermMatrix(corpus)

#?sample
mysample = dtm # no sampling (used Head on data read... for speed/simplicity on this example)
#mysample <- dtm[sample(1:nrow(dtm), 5000, replace=FALSE),]
#nrow(mysample)
wineSample = as.data.frame(as.matrix(mysample))

# column names (the words)
# use colnames to get a vector of the words
#colnames(wineSample)

# freq of words
# colSums to get the frequency of the words
#wineWordFreq = colSums(wineSample)

# structure in a way Tableau will like it
wordCloudData = data.frame(words=colnames(wineSample), freq=colSums(wineSample))
str(wordCloudData)

# sort by word freq
wordCloudDataSorted = wordCloudData[order(-wordCloudData$freq),]

# join together by ~ for processing once Tableau gets it
wordAndFreq = paste(wordCloudDataSorted[, 1], wordCloudDataSorted[, 2], sep = "~")

#write.table(wordCloudData, .fileOut, sep=",",row.names=FALSE) # if needed for performance refactors

topWords = head(wordAndFreq, .wordsToReturn)
#print(topWords)

return( topWords )

',
Max([Country Parameter])
, MAX([RowNum]) // for testing the grouping being sent to R
)

Tableau单词值的计算字段:

// grab the first token to the left of ~
Left([R Words+Freq], Find([R Words+Freq],"~") - 1)

表格频率值的计算字段:

INT(REPLACE([R Words+Freq],[Word]+"~",""))

如果您不熟悉Tableau,您可能希望与公司的Tableau分析师一起工作,他们将帮助您创建计算字段并配置Tableau以连接到Tabby

相关问题 更多 >