21

I want to replace the value of a dataframe cell using pandas. I'm using this line:

submission.iloc[i, coli] = train2.iloc[i2, coli-1]

I get the following error line:

IndexError: iloc cannot enlarge its target object

What is the reason for this?

Moradnejad
  • 3,466
  • 2
  • 30
  • 52

2 Answers2

29

I think this happens because either 'i' or 'coli' is out of bounds in submission. According to the documentation, you can Enlarge the dataframe with loc, meaning it would add the required row and column (in either axis) if you assign a value to a row/column that currently does not exist, but apparently iloc will not do the same.

Yashar
  • 762
  • 8
  • 16
2

As its documentation says, pandas dataframes are a data structure that contains labeled axes (rows and columns). iloc[] (and iat[]) are integer-location based indexers. Enlarging a dataframe means there will have to be a new index label for the enlarged part; however, iloc (and iat) being integer indexers wouldn't "understand" that.

loc[] and at[], being label-based indexers are OK.

df.loc[len(df)] = value
# or
df.at[len(df), 'col'] = value

Here the value of len(df) becomes the new label.


If the job is to add multiple rows to a dataframe, consider using concat() instead:

df = pd.concat([df, new_df])
cottontail
  • 10,268
  • 18
  • 50
  • 51