2017-09-30 231 views
0

我有一個熊貓系列在他們。我話的集合列出一串的數量出現想找到一個特定的詞的頻率在每個列表對於例如, 該系列產品是使用在熊貓系列

0 [All, of, my, kids, have, cried, nonstop, when... 
1 [We, wanted, to, get, something, to, keep, tra... 
2 [My, daughter, had, her, 1st, baby, over, a, y... 
3 [One, of, babys, first, and, favorite, books, ... 
4 [Very, cute, interactive, book, My, son, loves... 

我想要得到每行孩子的數量。我曾嘗試

series.count('kids') 

這給了我一個錯誤說「級別的孩子必須是相同的名稱(無)」

series.str.count('kids) 

給我NaN值。

我應該如何去獲取計數?

+0

如果你的問題得到回答,請[接受一個(幫助最多](ht TPS://stackoverflow.com/help/someone-answers)。 –

回答

2

使用

In [5288]: series.apply(lambda x: x.count('kids')) 
Out[5288]: 
0 1 
1 0 
2 0 
3 0 
4 0 
Name: s, dtype: int64 

詳細

In [5292]: series 
Out[5292]: 
0 [All, of, my, kids, have, cried, nonstop, when] 
1 [We, wanted, to, get, something, to, keep, tra] 
2 [My, daughter, had, her, 1st, baby, over, a, y] 
3  [One, of, babys, first, and, favorite, books] 
4 [Very, cute, interactive, book, My, son, loves] 
Name: s, dtype: object 

In [5293]: type(series) 
Out[5293]: pandas.core.series.Series 

In [5294]: type(series[0]) 
Out[5294]: list 
+0

加上1以將該文本轉換爲列表。 :) – Dark

+0

我對python有點新,所以請耐心等待。我使用split()將文本轉換爲列表。我嘗試使用lambda之前,但我得到了這個錯誤 –

+0

在做拆分之前,你可以實際使用'series.str.count('kids')' – Zero

1

在原系列,使用str.findall + str.len

print(series) 

0  All of my kids have cried nonstop when 
1  We wanted to get something to keep tra 
2  My daughter had her 1st baby over a y 
3  One of babys first and favorite books 
4 Very cute interactive book My son loves 

print(series.str.findall(r'\bkids\b')) 

0 [kids] 
1  [] 
2  [] 
3  [] 
4  [] 
dtype: object 

counts = series.str.findall(r'\bkids\b').str.len() 
print(counts) 

0 1 
1 0 
2 0 
3 0 
4 0 
dtype: int64