Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

df.at got error ValueError: Invalid call for scalar access (getting)

Tags:

python

pandas

df = pd.DataFrame({'a':[1,2,3],'b':[1,3,5]})
   a  b
0  1  1
1  2  3
2  3  5

when I use df.at[df['a']==1] it report error ValueError: Invalid call for scalar access (getting)!

but when I use df.at[df['a']==1,'b'] = 0 it works

   a  b
0  1  0
1  2  3
2  3  5
like image 641
xxyao Avatar asked Jan 25 '26 07:01

xxyao


1 Answers

When you call the Boolean just do simple slice will work.

df[df['a']==1]
   a  b
0  1  1

For your error .at only accept the pair index and col combination, notice the index and col should be int or array only contain one True value

The reason why you df.at[df['a']==1,'b'] = 0 work, since df['a']==1 return an array with only one True, which just same as input one index number.

If you change df.at[df['a'].isnull(),'b'] = 0 this will fail.

like image 165
BENY Avatar answered Jan 27 '26 20:01

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!