Reputation: 51
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:
df['dot2'] = df.groupby('dot1').split1.transform(lambda x : x.factorize()[0]+1)
Upvotes: 0
Views: 1256
Reputation: 30070
In df.groupby(d[i]).colnames[i]
, colnames[i]
is a variable. You may need do
df.groupby(d[i])[f'{colnames[i]}']
Upvotes: 1