[英]Counting the occurrence of one dataframe column as a substring in another?
I'm new to python and have found answers for counting hardcoded substrings in a df column but am unable to find an answer when using another df column as input. 我是python的新手,已经找到了在df列中计算硬编码子字符串的答案,但是在将另一个df列用作输入时却找不到答案。 Is this possible with pandas?
大熊猫有可能吗?
It's quite messy but essentially my dataframe is: 这很杂乱,但实际上我的数据框是:
ID Info
3457 <type1><stats></id>3457<type2></id>3457<type2></id>45
234 <type2><stats></id>234
4555 <type2><stats></id>604555<type1></id>4555<type2></id>4555
2378 <stats></id>555
I've managed to count the occurrences of specific strings eg 我设法计算出特定字符串的出现,例如
df['Type1_Count']=df['Info'].apply((lambda string: string.count("<type1>")))
df['Type2_Count']=df['Info'].apply((lambda string: string.count("<type2>")))
However I also need to count the occurrence of the ID's from the first column and since these can have a false match it would really need to be a count of the string "/id>" plus the ID column. 但是,我还需要从第一列开始计算ID的发生,由于这些ID可能存在错误匹配,因此实际上需要对字符串“ / id>”加上ID列进行计数。
Hope this makes sense, appreciate any help. 希望这有道理,感谢您的帮助。
You can try one of these 您可以尝试其中之一
df = pd.DataFrame({'name':['bernard','Samy','yyy'],'digit':[2,3,3],'SearchID':['be','xx','Sam']})
print df
for ID in df['SearchID']:
print ID, '\n', df.name.str.count(ID)
Searchstr = df['SearchID'].str.cat(sep='|')
print df.apply(lambda x: x['name'].count(x['SearchID']), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.