我已使用 pandas 将 CSV 文件导入到 Python。该文件由 3 列和 498 行组成。我只需要统计 1 个名为“Description”的列的字数。我通过将“描述”列转换为小写来清理文件,删除英语停用词和拆分。
在:
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 行,正如前面提到的,我需要计算词频。 任何帮助将不胜感激,感谢您的宝贵时间!
最佳答案
你的意思是这样吗?
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()
关于python - 用 Python 计算 Dataframe 中的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134773/