我有一个类似下面的数据报
lable unigrams
ham [ive, searching, right, word, thank, breather, i, promise, wont]
spam [free, entry, 2, wkly, comp, win, fa, cup, final, tkts, 21st, may]
我想计算不同的/独特的火腿字母组合和不同的垃圾邮件字母组合。
我可以使用df.unigrams.nunique()
计算一列中的不同值。
我可以使用unigramCount = unigramCorpus.loc["ham", "unigrams"].count('ive')
但是如何计算给定列表中不同值的数量?例如:["ham", "spam"]
预期输出: 火腿= 9 垃圾邮件= 12
答案 0 :(得分:2)
您需要:
$(".list").on("click", ".button", function() {
var x = $(this).closest(".item");
var y = $(".button").text();
$('#value').append('(' + y + ')');
});
答案 1 :(得分:1)
使用np.unique
(在每个字母组合列表中仅计数不同的词,因此重复项将被忽略):
df['counts'] = df.apply(lambda x: len(np.unique(x['unigrams'])), axis=1)
print(df)
> label unigrams counts
0 ham [ive, searching, right, word, thank, breather,... 9
1 spam [free, entry, 2, wkly, comp, win, fa, cup, fin... 12
答案 2 :(得分:0)
unigramCount = len(set(eval(unigramCorpus.loc [“ ham”,“ unigrams]])))
答案 3 :(得分:0)
您的问题不是很清楚,但这可能有用:
df['count'] = df['unigrams'].map(lambda x: len(x))