我有以下的數據幀DF:正確的方式使用ILOC在熊貓
print(df)
Food Taste
0 Apple NaN
1 Banana NaN
2 Candy NaN
3 Milk NaN
4 Bread NaN
5 Strawberry NaN
我試圖用ILOC在一個範圍內的行替換值:
df.Taste.iloc[0:2] = 'good'
df.Taste.iloc[2:6] = 'bad'
但它返回以下SettingWithCopyWarning消息:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
所以,我發現這個Stackoverflow page,並試圖這樣的:
df.iloc[0:2, 'Taste'] = 'good'
df.iloc[2:6, 'Taste'] = 'bad'
不幸的是,它返回以下錯誤:
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]
會是怎樣在這種情況下使用ILOC的正確方法?另外,有沒有辦法將上面這兩行結合起來?
不客氣,希望它有助於一點點。 –