I have the following dataframe df:
print(df)
Food Taste
0 Apple NaN
1 Banana NaN
2 Candy NaN
3 Milk NaN
4 Bread NaN
5 Strawberry NaN
I am trying to replace values in a range of rows using iloc:
df.Taste.iloc[0:2] = 'good'
df.Taste.iloc[2:6] = 'bad'
But it returned the following SettingWithCopyWarning message:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
So, I found this Stackoverflow page and tried this:
df.iloc[0:2, 'Taste'] = 'good'
df.iloc[2:6, 'Taste'] = 'bad'
Unfortunately, it returned the following error:
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]
What would be the proper way to use iloc in this situation? Also, is there a way to combine these two lines above?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…