【发布时间】:2019-12-09 17:38:30
【问题描述】:
我想在推文数据集中查找不同语言的频率。我最终只想使用英文的推文,但也想找出其他语言的频率。
我已经使用 langdetect 在我的数据集中检测到推文的语言,现在我想计算每种语言的频率。这是我检测语言的代码:
from langdetect import detect
import pandas as pd
data_path = "./output_1.csv"
df = pd.read_csv(data_path, index_col=0)
for index, row in df.iterrows():
print(detect(row['text']))
if detect(row['text']) == "en":
print(row['text'])
我想使用列表属性计数来计算频率:
using the list i = ['en','fr','es','it','cs','pt']
d = {x:i.count(x) for x in i}
print d
如何对使用 langdetect 获得的数据使用 count 属性?
【问题讨论】:
-
您有一个名为
text的列,其值为语言?在那种情况下可能是pd.Series.value_counts -
@ALollz 我正在努力创建一个单独的列,其中仅包含语言检测的输出。我试过
df["language"] = (detect(row['text']))但这只是使用第一行而不是单独使用它们。你有什么建议吗?