AttributeError:“ str”对象没有属性“ size”

时间:2019-09-11 12:54:10

标签: python pandas numpy

这与我以前的问题有关,未回答,来自这里:enter image description here

for row in range(len(df)):
    while df["Full_Input_Length"][row] >= 55:
        df["Input3"][row] = np.random.choice(sentence_list, size=len(df))
        df["Full_Input"][row] = np.array2string(df['TitleTag'][row]).replace("'","") +  " " + np.array2string(df['Input2'][row]).replace("'","") + " " + np.array2string(df['Input3'][row]).replace("'","") + " " +  np.array2string(df['Input4'][row]).replace("'","") + " " +  np.array2string(df['Input5'][row]).replace("'","") 
        df["Full_Input_Length"][row] = len(df["Full_Input"][row])
    break

我坚持最初的计划,并继续尝试正确地写下该for循环。我遇到的错误是在第4行,显示为

  

AttributeError:'str'对象没有属性'size'

基本上,我试图将多个字符串连接到Full_Input列中。我以为现在问题出在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

0 个答案:

没有答案