Fuad Ak
Fuad Ak

Reputation: 158

'DataFrame' object has no attribute 'col' error

First 2 rows of loop works but last row df[col] gives error. I get 'DataFrame' object has no attribute 'col' error

   df = pd.DataFrame([(.2, np.nan,), (.0, .6, 2), (.6, .0, 1), (.2, .1, 1 )],
                  columns=['dogs', 'cats','monkeys'])

corr=df.corr(method='pearson')

for col in corr.columns:
    print ('col:', col)
    print(df[col])
    df[col]=df[col].fillna(corr.col.mean())

Upvotes: 0

Views: 823

Answers (1)

jezrael
jezrael

Reputation: 862431

Use [col] for select column by variable:

for col in corr.columns:
    print ('col:', col)
    print(df[col])
    df[col]=df[col].fillna(corr[col].mean())

Better and simplier solution is pass mean to DataFrame.fillna :

df = df.fillna(df.corr(method='pearson').mean())

Upvotes: 1

Related Questions