简体   繁体   English

将一个数据框列的出现计为另一个中的子字符串?

[英]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.

相关问题 计算另一列中一列的子串的出现次数 - Counting the occurrences of a substring from one column within another column 如何在一个 Pandas 数据帧列中搜索字符串作为另一个数据帧中的子字符串 - How to search a string in one pandas dataframe column as a substring in another dataframe 计算从 Dataframe 的一列到另一列 Dataframe 的单词 - Counting Words from one columns of Dataframe to Another Dataframe column 比较一个 dataframe 列中的行 substring 与另一个 dataframe 列的行 substring,并删除不匹配的 - Compare a row substring in one dataframe column with row substring of another dataframe column, and remove non-matching ones 使用字符串列表计算 dataframe 列中单词的出现次数 - Counting the occurrence of words in a dataframe column using a list of strings 检查一个数据帧中的一列字符串是否包含另一个数据帧中一列的子字符串,并输出其映射数据 - Check if a column of strings from one dataframe contains a substring from a column in another dataframe, and output its mapped data 如何在一个 Pandas 数据帧列中搜索字符串作为另一个数据帧列中的子字符串 - How to search a string in one pandas dataframe column as a substring in another dataframe column 如果数据框存在于另一个数据框列中,则搜索它的子字符串 - Searching substring of a dataframe if it exists in another dataframe column 如何从 dataframe 中的一列中剪切 substring 并与另一列中的字符串连接? - How to cut a substring from one column in dataframe and concat with string in another column? 按年龄计算数据框中性别的出现次数 - Counting occurrence of a gender in a dataframe by age
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM