【问题标题】:Counting the number of times each word appears in a range of excel cells计算每个单词在一系列 Excel 单元格中出现的次数
【发布时间】:2021-06-18 21:40:02
【问题描述】:

查询:我想统计某个单词在excel单元格范围内出现的次数

如果可能,不要寻找某个单词,而是使用一个脚本来总结 Excel 单元格范围内每个单词出现的次数。即按照频率从高到低的顺序总结,每个单词出现的次数。

Excel 文件:

  1. 单元格范围在 K 列中
  2. 每个单元格有 >1000 个字符
  3. 有 >1000 行

谢谢!

【问题讨论】:

  • 你能告诉我们你到目前为止得到了什么吗?
  • import pandas as pd # 在此处插入您的文件名 filename = r"C:\Users\compl\OneDrive\Desktop\Documents"+"\\"+ "TEST" # Excel 列搜索 columname = "Notes1" # 要搜索的关键字 Keyword = "The" df = pd.read_excel(filename) #print(df) #condensed = df[df[columname].str.contains(Keyword, na=False)] print(condensed [columname].str.split(expand=True).stack().value_counts()) 返回文件未找到错误。我对此很陌生,所以任何帮助都会很棒,谢谢!
  • 对你来说重要的是它是用python制作的还是VBA可以?如果您必须为很多工作表执行此操作,这不是一个好的选择,如果您只需要在几个工作表中执行此操作,它可能是一个可行的选择,因为它易于编写并且全部在 excel 中。
  • 只有 12 张,如果可能的话,如果你有一个脚本可以在 VBA 中做吗?谢谢!
  • 我想我可以想出一些东西,我最近对一个脚本做了类似的事情。单词是否仅由空格分隔,或者是否还有点和 - 或 ;正确拆分单词很重要,否则您可能会有“某物”和“某物”。输入结果

标签: python excel


【解决方案1】:

首先,您需要一个函数来删除从单元格中获取的字符串中的所有特殊字符。

Function without_special_chars(text As String) As String
Dim i As Integer
Const special_chars As String = "-.,:;#+ß'*?=)(/&%$§!~\}][{"
For i = 1 To Len(special_chars)
text = Replace(text, Mid(special_chars, i, 1), "")
Next i
without_special_chars = text
End Function

这只是一个查看字符串是否已经在数组中的函数。

Function IsInArray(stringToBeFound, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

然后你可以循环遍历所有的单元格。

Sub query()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tabelle1")
Dim wordlist()
Dim nList()
Dim word As String
For i = 1 To ws.Cells(1048576, 11).End(xlUp).Row
    text = without_special_chars(ws.Cells(i, 11).Value)
    cell_words = Split(text, " ")
    For j = 0 To UBound(cell_words)
        the_word = cell_words(j)
        If j = 0 And i = 1 Then
            ReDim wordlist(0)
            ReDim nList(0)
            wordlist(UBound(wordlist)) = the_word
            nList(UBound(nList)) = 1
        Else
            If IsInArray(the_word, wordlist) = True Then
                For n = 0 To UBound(wordlist)
                    If wordlist(n) = the_word Then
                        nList(n) = nList(n) + 1
                        Exit For
                    End If
                Next
            Else
                ReDim Preserve wordlist(UBound(wordlist) + 1)
                ReDim Preserve nList(UBound(nList) + 1)
                wordlist(UBound(wordlist)) = the_word
                nList(UBound(wordlist)) = 1
            End If
        End If
    Next
Next
For m = 0 To UBound(wordlist)
    ws.Cells(m + 1, 1).Value = wordlist(m)
    ws.Cells(m + 1, 2).Value = nList(m)
Next
End Sub

对于 ws,您必须输入工作表的名称。 最后一个带有 m 的 for 循环是输出,我只是为了测试目的而制作它,最后你将有两个数组。存储所有单词的 wordlist 和存储数量的 nList。我没有考虑大小写,所以“I”和“i”可能有两个条目。

您只需将此代码放在 VBA 编辑器中并运行查询子程序。 (打开Excel,按Alt+F11,放到ThisWorkbook中,点击sub的一行,点击运行)

VBA 不是很快,所以我不知道需要多长时间,但我用几百个单元格和一些句子对其进行了测试,大约需要 1.5 分钟。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多