0

I was new to python and lately I'm having trouble dealing with pandas. It always pops warnings like this: "A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["第一题"][index] = df["第一题"][index] + 1"

And my code is: enter image description here

So the results end with code 0, and indeed all value in column "第一题" been added 1, but the warning is still there, so I changed the code to: for index in range(len(df["第一题"])): df.loc[df["第一题"][index]] = df.loc[df["第一题"][index] + 1]

The warning is gone now, but the values didn't change, which means, the values didn't add 1 as the operation commanded. Even when I add df = df.copy(), the problem is still there.

So may I know how do I deal with this? Thank you all for help!:)

2 Answers2

0

You'd want to copy the whole df I believe.

df = df.copy()
for index in range(len(df["第一题"])):
    # rest of the code
Frankfurters
  • 108
  • 8
0

Use this code:

indexer = df.get_indexer(["第一题"])[0]
for index in range(len(df)):
    df.iloc[index, indexer] = df.iloc[index, indexer] + 1

Or, even better (and so much faster!) would be:

df["第一题"] += 1