2017-08-10 36 views
1

我想計算從csv文件單元格中出現list_of_strings中的任何字符串的次數。有沒有辦法使用str.count()函數與值的LIST而不是單個字符串?

例如,以下工作正常。

import pandas as pd 

data_path = "SurveryResponses.csv" 

df = pd.read_csv(data_path) 

totalCount = 0 
for row in df['rowName']: 
    if type(row) == str: 
     print(row.count('word_of_interest')) 

不過,我想能夠進入字符串列表([「STR1」,STR2' ,STR3' ]),而不僅僅是一個‘word_of_interest’,這樣,如果其中任何字符串出現計數值將增加1。

有沒有辦法做到這一點?

+0

是否有任何問題的答案提供瞭解決問題了嗎? – DJK

回答

0

也許沿

totalCount = 0 
words_of_interst = ['cat','dog','foo','bar'] 

for row in df['rowName']: 
    if type(row) == str: 
     if sum([word in row for word in words_of_interst]) > 0: 
      totalCount += 1 
0

東西線使用STR訪問:如果您需要將列字符串先轉換

df['rowName'].str.count('word_of_interest') 

,使用astype:

df['rowName'].astype(str).str.count('word_of_interest') 
0

假設list_of_strings = ['str1', str2', str3']您可以嘗試以下操作:

if any(map(lambda x: x in row, list_of_strings)): 
    totalCount += 1 
0

您可以使用此方法從外部列表

strings = ['string1','string2','string3'] 
sum([1 if sr in strings else 0 for sr in df.rowName]) 
0

這裏來算就是一個例子:

import io 

filedata = """animal,amount 
"['cat','dog']",2 
"['cat','horse']",2""" 

df = pd.read_csv(io.StringIO(filedata)) 

返回此數據框:

animal   amount 
0 ['cat','dog'] 2 
1 ['cat','horse'] 2 

搜索詞貓(作爲系列通過所有列循環):

search = "cat" 

# sums True for each serie and then wrap a sum around all sums 
# sum([2,0]) in this case 

sum([sum(df[cols].astype(str).str.contains(search)) for cols in df.columns]) 

返回2

相關問題