在datafram上分组时出错

2024-09-28 21:54:59 发布

您现在位置:Python中文网/ 问答频道 /正文

你能给我以下建议吗,我有点困了

因此,dataframe3有“domain”和“size”列。我的脚本清理域并添加一个名为“newdomain2”的新列

我在下面添加一列&;查看数据帧;看起来是对的

因此,df4需要是df3的聚合版本(按域+总和(大小)分组),但当我尝试下面的方法时,会出现以下错误:

TypeError:无法处理的类型:“list”

我应该注意,如果我在同一个脚本中使用'domain'而不是'cleandomain2',它就可以正常工作

你能帮我理解为什么会这样吗

 df3['newdomain2']=cleandomain
 #show df3
 df3

 df4 = df3.groupby(['newdomain2'])[['size']].sum()

下面是我用来生成新列值的脚本;将值添加到数据帧

for x in index:
     #if it ends with a number, it's an IP
     if str(x[len(x)-1]).isnumeric():
         cleandomain.append(str(x[0])+'.'+str(x[1])+'.*.*')
     #if its in the CDN list, take a subdomain as well
     elif str(x[len(x)-2]).rstrip() in cdns:
         cleandomain.append(str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+str(x[len(x)-1]))
     elif str(x[len(x)-3]).rstrip() in cdns:
         cleandomain.append(str(x[len(x)-4])+'.'+str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
    #if its in the TLD list, do this
     elif str(x[len(x)-2]).rstrip()+'.'+ str(x[len(x)-1]).rstrip() in tld:
         cleandomain.append(str(x[len(x)-3])+'.'+str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
     elif str(x[len(x)-1]) in tld:
         cleandomain.append(str(x[len(x)-2])+'.'+ str(x[len(x)-1]))
    #if its not in the TLD list, do this
     else:
         cleandomain.append(x)
 #add column do df3
 df3['newdomain2']=cleandomain

Tags: thein脚本lenifdolistits