Fernando Chu
Fernando Chu

Reputation: 383

Can't edit dataframe data through iloc in pandas

So something really weird is happening when I try editing :

In [119]: print(GDP.iloc[1][0])
Out [119]: Andorra

When I try to edit it with .iloc and query it again this happens:

In [120]: GDP.iloc[1][0]="Cats"
          print(GDP.iloc[1][0])
Out [120]: Andorra

I remember reading that .iloc may call a copy or an image depending on the numpy type. Anyway to fix this or is there other way I should be editing my data? Thanks.

Upvotes: 2

Views: 592

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

It is best to avoid chaining assignments in pandas, see this SO post which refers to this Pandas doc about chaining assignments

Whenever, you have "][" in pandas it is generally bad and should be rewritten.

It is best written as Divakar suggests:

GDP.iloc[1,0]="Cats"

Upvotes: 3

Related Questions