Python 属性错误:';str';对象没有属性';尺寸';
这与我之前的问题有点关联,没有回答,从这里: 我坚持我最初的计划,并继续尝试正确地写下这个for循环。我得到的错误在第4行,上面写着 AttributeError:“str”对象没有属性“size” 基本上,我试图将多个字符串连接到列Full_输入中。我假设现在问题出在Python 属性错误:';str';对象没有属性';尺寸';,python,pandas,numpy,Python,Pandas,Numpy,这与我之前的问题有点关联,没有回答,从这里: 我坚持我最初的计划,并继续尝试正确地写下这个for循环。我得到的错误在第4行,上面写着 AttributeError:“str”对象没有属性“size” 基本上,我试图将多个字符串连接到列Full_输入中。我假设现在问题出在df[“Full_Input”][row]上,但我不太确定如何正确编写它,这样我的代码才能运行。我尝试了不同的方法,但没有成功——其他错误突然出现 我做错了什么?还有别的方法吗 完整代码: df['TitleTag'] =
df[“Full_Input”][row]
上,但我不太确定如何正确编写它,这样我的代码才能运行。我尝试了不同的方法,但没有成功——其他错误突然出现
我做错了什么?还有别的方法吗
完整代码:
df['TitleTag'] = np.nan
for col in range(len(df)):
condlist = [df['BrandId'][col] != 2, df['BrandId'][col] == 2]
choicelist = [df['Name'][col] + ' fra ' + df['BrandName'][col], df['Name'][col]]
df['TitleTag'][col] = np.select(condlist, choicelist)
df['Input1'] = np.nan
for col in range(len(df)):
condlist = [df['BrandId'][col] != 2, df['BrandId'][col] == 2]
choicelist = [df['Name'][col] + ' fra ' + df['BrandName'][col], np.nan]
df['Input1'][col] = np.select(condlist, choicelist)
symbol_list = (['-','=>','|','->'])
df["Input2"] = np.random.choice(symbol_list, size=len(df))
sentence_list = (['Køb online her','Sammenlign priser her','Tjek priser fra 4 butikker','Se produkter fra 4 butikker', 'Stort udvalg fra 4 butikker','Sammenlign og køb'])
df["Input3"] = np.random.choice(sentence_list, size=len(df))
symbol_list2 = (['-','|'])
df["Input4"] = np.random.choice(symbol_list2, size=len(df))
df["Input5"] = "Site.dk"
df["Full_Input"] = df['TitleTag'].astype(str) + " " + df['Input2'].astype(str) + " " + df['Input3'].astype(str) + " " + df['Input4'].astype(str) + " " + df['Input5'].astype(str)
df["Full_Input_Length"] = df["Full_Input"].apply(len)
for col in range(len(df)):
while df["Full_Input_Length"][col] >= 55:
df["Input3"][col] = np.random.choice(sentence_list, size=len(df))
df["Full_Input"][col] = np.array2string(df['TitleTag'][col]).replace("'","") + " " + np.array2string(df['Input2'][col]).replace("'","") + " " + np.array2string(df['Input3'][col]).replace("'","") + " " + np.array2string(df['Input4'][col]).replace("'","") + " " + np.array2string(df['Input5'][col]).replace("'","")
df["Full_Input_Length"][col] = len(df["Full_Input"][col])
break
df['TitleTag'][row]
和您连接的其他变量是什么样子的?df['TitleTag'][1]Out[179]:数组('Mineslunder m.ramsløgøko.fra Mineslund',dtype='In[181]:np.array2string(df['TitleTag'][col])。替换(“,”)Out[181]:'Mineslunder m.ramsløgøko.fra Mineslund'
如果单独工作正常,则错误不在代码的那一行。如果您可以粘贴堆栈跟踪和更多代码,可能会有所帮助。我只是注意到在尝试连接它们时出现问题。例如:np.array2string(df['TitleTag'][col])。替换(“,”)+np.array2string(df['Input2'][col])。替换(“,”)
。我将用完整的代码编辑原始帖子。
df['TitleTag'] = np.nan
for col in range(len(df)):
condlist = [df['BrandId'][col] != 2, df['BrandId'][col] == 2]
choicelist = [df['Name'][col] + ' fra ' + df['BrandName'][col], df['Name'][col]]
df['TitleTag'][col] = np.select(condlist, choicelist)
df['Input1'] = np.nan
for col in range(len(df)):
condlist = [df['BrandId'][col] != 2, df['BrandId'][col] == 2]
choicelist = [df['Name'][col] + ' fra ' + df['BrandName'][col], np.nan]
df['Input1'][col] = np.select(condlist, choicelist)
symbol_list = (['-','=>','|','->'])
df["Input2"] = np.random.choice(symbol_list, size=len(df))
sentence_list = (['Køb online her','Sammenlign priser her','Tjek priser fra 4 butikker','Se produkter fra 4 butikker', 'Stort udvalg fra 4 butikker','Sammenlign og køb'])
df["Input3"] = np.random.choice(sentence_list, size=len(df))
symbol_list2 = (['-','|'])
df["Input4"] = np.random.choice(symbol_list2, size=len(df))
df["Input5"] = "Site.dk"
df["Full_Input"] = df['TitleTag'].astype(str) + " " + df['Input2'].astype(str) + " " + df['Input3'].astype(str) + " " + df['Input4'].astype(str) + " " + df['Input5'].astype(str)
df["Full_Input_Length"] = df["Full_Input"].apply(len)
for col in range(len(df)):
while df["Full_Input_Length"][col] >= 55:
df["Input3"][col] = np.random.choice(sentence_list, size=len(df))
df["Full_Input"][col] = np.array2string(df['TitleTag'][col]).replace("'","") + " " + np.array2string(df['Input2'][col]).replace("'","") + " " + np.array2string(df['Input3'][col]).replace("'","") + " " + np.array2string(df['Input4'][col]).replace("'","") + " " + np.array2string(df['Input5'][col]).replace("'","")
df["Full_Input_Length"][col] = len(df["Full_Input"][col])
break