【问题标题】:Is it possible to get a detailed list of word frequencies from Pandas Profiling?是否可以从 Pandas Profiling 中获得详细的词频列表?
【发布时间】:2021-08-14 08:05:29
【问题描述】:

我目前正在处理大量文件,这些文件需要我检查某些字符串的频率。我的第一个想法是将所有文件导入到单个数据集中,并使用 for 循环使用以下代码检查所有文件中的字符串。

 # Define an empty dataframe to append all imported files to
df = pd.DataFrame()
new_list = []

# If text file is import successfully append the resulting dataframe to df. If an exception occurs append "None" instead.
# "`" was chosen as the delimiter to ensure that each file is saved to a single row.
for i in file_list: 
    try: df_1 = pd.read_csv(f"D:/Admin/3. OCR files/OCR_Translations/{i}", delimiter = "`") 
    df = df.append(df_1) new_list.append(f"D:/Admin/3. OCR files/OCR_Translations/{i}") 
except: 
    df = df.append(["None"])                
    new_list.append("None")

df = df.T.reset_index()

# Search the dataset for the required keyword
count = 0

for i in df["index"]:
    if "Keyword1" in i:
        count += 1

这最终失败了,因为绝对零保证字符串在这些文件中的拼写正确,因为相关文件是由 OCR 程序生成的(该文件和相关文件都是泰语)。

Pandas Profiling 准确地生成了我手头工作所需的内容,但它没有提供完整列表,如此链接 (https://imgur.com/xxf1Qnx) 中所示。有没有办法从 Pandas Profiling 中获取完整的词频列表?我已经尝试检查 pandas_profiling 文档 (https://pandas-profiling.github.io/pandas-profiling/docs/master/index.html) 以查看是否有任何我可以做的事情,但到目前为止我还没有看到任何与我的用例相关的内容。

【问题讨论】:

    标签: python pandas pandas-profiling


    【解决方案1】:

    可能不需要不需要 Pandas 来计算文件中的单词出现次数。

    import collections
    
    word_counter = collections.Counter()
    
    for i in file_list:
        with open(f"D:/Admin/3. OCR files/OCR_Translations/{i}") as f:
            for line in f:
                words = line.strip().split()  # Split line by whitespaces.
                word_counter.update(words)  # Update counter with occurrences.
    
    
    print(word_counter)
    

    您可能还对 Counters 上的 .most_common() 方法感兴趣。

    另外,如果你真的需要,你也可以把Counter转成数据框;它只是一个带有特殊效果的字典。

    【讨论】:

    • 非常感谢您的回答。原来这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多