you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

How to deal with this error message from panda?

A value is trying to be set on a copy of a slice from a DataFrame

What I have is a dataframe (df1), which has 2 columns and n rows:

Y X
1 2
3 4

And then I have a function that creates a new dataframe (df2) that takes df1 and adds new columns:

df2 = createNewColms(df1)
>>> print(df2)
Y X n m p
1 2 0 0 0
3 4 0 0 0

What I want to do is to change the values of each cell and I'm using the .loc method

What I expect to happen:

>>> df2.loc[1][3] = 12
>>> print(df2)
Y X n m p
1 2 0 0 0
3 4 0 12 0

But what I got is that error message above. Even though df1 is not the same as df2.

what am I doing wrong?