简体   繁体   English

AttributeError: 'DataFrameGroupBy' object 没有属性 'colnames'

[英]AttributeError: 'DataFrameGroupBy' object has no attribute 'colnames'

I have a list of column names as shown below:我有一个列名列表,如下所示:

colnames = ['split1', 'split2', 'split3', 'split4', 'split5', 'split6', 'split7']

I have used this in a line of code where I iterate over the list items as shown below:我在一行代码中使用了它,在其中迭代列表项,如下所示:

for i in range(0,x+2):
    df[d[i+1]] = df.groupby(d[i]).colnames[i].transform(lambda x : x.factorize()[0]+1)

This is giving me the attribute error:这给了我属性错误:

AttributeError: 'DataFrameGroupBy' object has no attribute 'columns'

But if I individually give the colname items to the code, it works fine:但是,如果我将 colname 项单独提供给代码,则可以正常工作:

df['dot2'] = df.groupby('dot1').split1.transform(lambda x : x.factorize()[0]+1)

In df.groupby(d[i]).colnames[i] , colnames[i] is a variable.df.groupby(d[i]).colnames[i]中, colnames[i]是一个变量。 You may need do你可能需要做

df.groupby(d[i])[f'{colnames[i]}']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM