Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将一个数据帧列的出现计数为另一个数据帧列中的子字符串?_Python_Python 2.7_Pandas_Substring - Fatal编程技术网

Python 是否将一个数据帧列的出现计数为另一个数据帧列中的子字符串?

Python 是否将一个数据帧列的出现计数为另一个数据帧列中的子字符串?,python,python-2.7,pandas,substring,Python,Python 2.7,Pandas,Substring,我是python新手,已经找到了在df列中计算硬编码子字符串的答案,但在使用另一个df列作为输入时找不到答案。这对熊猫有可能吗 这相当混乱,但基本上我的数据帧是: ID Info 3457 <type1><stats></id>3457<type2></id>3457<type2></id>45 234 <type2><stats></id>234 4555 &l

我是python新手,已经找到了在df列中计算硬编码子字符串的答案,但在使用另一个df列作为输入时找不到答案。这对熊猫有可能吗

这相当混乱,但基本上我的数据帧是:

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
ID信息
3457  3457345745
234   234
4555  60455545554555
2378  555
我已经设法计算了特定字符串的出现次数,例如

df['Type1_Count']=df['Info'].apply((lambda string: string.count("<type1>")))
df['Type2_Count']=df['Info'].apply((lambda string: string.count("<type2>")))
df['Type1_Count']=df['Info'].apply((lambda字符串:string.Count(“”))
df['Type2_Count']=df['Info'].apply((lambda字符串:string.Count(“”))
但是,我还需要从第一列开始计算ID的出现次数,因为这些ID可能存在错误匹配,所以实际上需要对字符串“/ID>”加上ID列进行计数


希望这有意义,感谢您的帮助。

您可以尝试其中一种

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)

对于第一个方法,我得到了错误“TypeError:第一个参数必须是字符串或编译模式”,对于第二个方法,我得到了“AttributeError:只能使用带字符串值的.str访问器,它在pandas中使用np.object dtype”,可能是因为我的SearchID是int64?是否需要转换数据类型?是的,此类函数需要字符串,请将其转换为字符串,然后重试