Reputation: 1634
I am using KNeighborsClassifier
to classify some values like this:
arr = classifier_3NN.predict(testData_df)
len(arr)
10960
I want to assign this array to a column in a DataFrame, I have checked & it is the same size:
len(df[df['events']=='NaN']['events'])
10960
When I do the following command the array values are not in the column as expected:
df[df['events']=='NaN']['events'] = arr
Can anyone see what I'm doing wrong?
Upvotes: 2
Views: 2223
Reputation: 862661
I think you need isnull
for checking NaN
values with ix
:
df.ix[df['events'].isnull(), 'events'] = arr
But if need replace NaN
values by arr
better is use fillna
by Series
created from arr
:
arr = [5,7,9]
df = pd.DataFrame({'events':[np.nan, 1, np.nan]})
print (df)
events
0 NaN
1 1.0
2 NaN
df['events'] = df['events'].fillna(pd.Series(arr, index=df.index))
print (df)
events
0 5.0
1 1.0
2 9.0
Upvotes: 1