You can also change the underlying array of a cell.
values
/to_numpy()
returns a view of the underlying array of a DataFrame, so if a particular value in the array is changed, the change is reflected on the DataFrame as well.
df = pd.DataFrame(index=['A','B','C'], columns=['x','y'])
# change the last value in the first column
df.values[-1, 0] = 10
df.to_numpy()[-1, 0] = 10
x y
A NaN NaN
B NaN NaN
C 10 NaN
You can also select a column, view its underlying array and change it by index as well. This method works even if the dtype is Extension Dtype.
# change the last value in column 'x'
df['x'].values[-1] = 100
Changing the DataFrame view the fastest way (5 times faster than the next fastest method) to set a value in a cell, which becomes relevant if this is done in a loop.
df = pd.DataFrame(index=['A', 'B', 'C'], columns=['x', 'y'])
%timeit df.values[-1, 0] = 10 # 1.89 µs ± 85.1 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
%timeit df.iat[-1, 0] = 10 # 10.9 µs ± 380 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
%timeit df.at['C', 'x'] = 10 # 13 µs ± 307 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
%timeit df.loc['C', 'x'] = 10 # 55.4 µs ± 6.16 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit df.iloc[-1, 0] = 10 # 39.7 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
df['x']['C']
), usedf.ix['x','C']
. – Herbydataframe[column (series)] [row (Series index)]
, whereas many people (including myself) are more used to thedataframe[row][column]
order. As a Matlab and R programmer the latter feels more intuitive to me but that apparently is not the way Pandas works.. – Intercellular