I have a pandas dataframe:
df = pd.DataFrame({'AKey':[1, 9999, 1, 1, 9999, 2, 2, 2],\
'AnotherKey':[1, 1, 1, 1, 2, 2, 2, 2]})
I want to assign a new value to a specific column and for each element having a specific value in that column.
Let say I want to assign the new value 8888
to the elements having value 9999
.
I tried the following:
df[df["AKey"]==9999]["AKey"]=8888
but it returns the following error:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
So I tried to use loc
df.loc[df["AKey"]==9999]["AKey"]=8888
which returned the same error.
I would appreciate some help and some explanation on the error as I really can't wrap my head around it.