0

If I initialize a new column to zeros:

df['key'] = 0

then:

df.loc[0]['key']
Out[538]: 0

but when I set it to a new value:

df.loc[0]['key'] = 1

Calling that entry still returns zero...

df.loc[0]['key']
Out[538]: 0

What is going on?

hirschme
  • 774
  • 2
  • 11
  • 40

1 Answers1

4

df.loc[0]['key'] is a slice of df.loc[0], so you're setting a value on a slice, not the dataframe itself. Conversely .loc and .iloc refer back to the dataframe itself, so you should be able to update the original value if you call df.loc[0, 'key']=1.

Jake Morris
  • 645
  • 6
  • 18