我正在嘗試將索引 3-7 中的“D”值設置為 0,這是我收到的代碼,但出現錯誤。df.iloc[3:7, 'D'] = 0我也試過:df.iloc[3:7]['D'] = 0 A B C D
0 foo one small 1
1 foo one large 2
2 foo one large 2
3 foo two small 3
4 foo two small 3
5 bar one large 4
6 bar one small 5
7 bar two small 6
8 bar two large 7
3 回答

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
嘗試df.loc[df.index[3:7], "D"] = 0
編輯:我認為這會起作用!但請確保您的索引設置為索引。
mask?=?(df.index?>=3)?&?(df.index?<=7) df.loc[mask,?"D"]?=?0
添加回答
舉報
0/150
提交
取消