首页 > 解决方案 > 在 Python 中计算数据框中的单词

问题描述

我已经使用 pandas 将 CSV 文件导入 Python。该文件由 3 列和 498 行组成。我只需要一个名为“描述”的列的字数。我已经通过将“描述”列转换为小写,删除英文停用词和拆分来清理文件。

输入

    import pandas as pd

    df = pd.read_csv("capex_motscles.csv")

    from nltk.corpus import stopwords
    stop = stopwords.words('english') 

    Description3 = df['Description'].str.lower().apply(lambda x: 
    ''.join([word for word in str(x).split() if word not in (stop)]))

    print(Description3)

输出

    0      crazy mind california medical service data base...
    1      california licensed producer recreational & medic...
    2      silicon valley data clients live beyond status...
    3      mycrazynotes inc. announces $144.6 million expans...
    4      leading provider sustainable energy company prod ...
    5      livefreecompany founded 2005, listed new york stock...

我从“print(Description3)”中提供了 5 行。我总共有 498 行,如前所述,我需要计算词频。任何帮助将不胜感激,感谢您的宝贵时间!

标签: pythonpython-3.xpandasnltkstop-words

解决方案


你的意思是这样的吗?

df['Description3'] = df['Description'].str.lower().apply(lambda x: 
                             ''.join([word for word in str(x).split() if word not in (stop)]))

df['Description3'].str.split(expand=True).stack().value_counts()

推荐阅读