我有一个数据帧:
name sample
1 a Category 1: qwe, asd (line break) Category 2: sdf, erg
2 b Category 2: sdf, erg(line break) Category 5: zxc, eru
...
30 p Category 1: asd, Category PE: 2134, EFDgh, Pdr tke, err
最后我想说:
name qwe asd sdf erg zxc eru 2134 EFDgh Pdr tke err
1 a 1 1 1 1 0 0 0 0 0 0
2 b 0 0 1 1 1 1 0 0 0 0
...
30 p 0 1 0 0 0 0 0 1 1 0
我创建了以下函数:
def cleanattributes(istring):
istring=str(istring)
istring=istring.rstrip().split('\\n')
counter=0
for line in istring:
istring[counter]=istring[counter].rpartition(': ')[-1]
counter+=1
istring=str(istring)
istring = istring.replace("'", "")
istring = istring.replace("\"", "")
return(str(istring))
此函数创建返回类别信息而不返回类别标题的预期结果(其思想是使用getdummies获取列)
teststring="Category 1: qwe, asd\\nCategory 2: sdf, erg"
cleanattributes(teststring)
OUTPUT: '[qwe, asd, sdf, erg]'
我不确定如何最好地将此函数应用于每条记录,以便数据帧如下所示:
name sample
1 a qwe, asd, sdf, erg
2 b sdf, erg, zxc, eru
...
30 p asd, 2134, EFDgh, Pdr tke, err
或者这是最好的方法。你知道吗
按要求:
df['sample'].iat[0]
OUTPUt= 'Category 1: qwe, asd\nCategory 2: sdf, erg'
相关问题 更多 >
编程相关推荐