Why does
>>> offset = 2
>>> data = {'Value': [7, 9, 21, 22, 23, 100]}
>>> df = pd.DataFrame(data=data)
>>> df.at[:offset, "Value"] = 99
>>> df
Value
0 99
1 99
2 99
3 22
4 23
5 100
change values in indices [0, 1, 2]? I would expect them only to be changed in [0, 1] to be conform with regular slicing.
Like when I do
>>> arr = [0, 1, 2, 3, 4]
>>> arr[0:2]
[0, 1]
CodePudding user response:
.at
behaves like .loc
, in that it selects rows/columns by label. Label slicing in pandas is inclusive. Note that .iloc
, which performs slicing on the integer positions, behaves like you would expect. See this good answer for a motivation.
Also note that the pandas documentation suggests to use .at only when selecting/setting single values. Instead, use .loc
.
CodePudding user response:
On line 4, when you say :2
, it means all rows from 0 to 2 or 0:2
. If you want to change only the 3rd row, you should change it to 2:2