We are aware that the standard method of setting a single cell is using at
or iat
. However, I noticed some interesting behaviour I was wondering if anyone could rationalise.
In solving this question, I come across some weird behaviour of loc
.
# Setup.
pd.__version__
# '0.24.0rc1'
df = pd.DataFrame({'A': [12, 23], 'B': [['a', 'b'], ['c', 'd']]})
df
A B
0 12 [a, b]
1 23 [c, d]
To set cell (1, 'B'), it suffices to do this with at, like df.at[1, 'B'] = ...
. But with loc, I initially tried this, which did not work:
df.loc[1, 'B'] = ['m', 'n', 'o', 'p']
# ValueError: Must have equal len keys and value when setting with an iterable
So, I tried (which also failed)
df.loc[1, 'B'] = [['m', 'n', 'o', 'p']]
# ValueError: Must have equal len keys and value when setting with an ndarray
I thought loc
would also somehow be able to take nested lists here. In a bizarre turn of events, this code worked:
df.loc[1, 'B'] = [['m'], ['n'], ['o'], ['p']]
df
A B
0 12 [a, b]
1 23 [m, n, o, p]
Why does loc
work this way? Additionally, if you add another element to any of the lists, it flops:
df.loc[1, 'B'] = [['m'], ['n'], ['o'], ['p', 'q']]
# ValueError: Must have equal len keys and value when setting with an iterable
Empty lists don't work either. It seems pointless to have to nest each element in its own list.
Why does loc
do this? Is this documented behaviour, or a bug?
See Question&Answers more detail:
os