Pandas error: "IndexError: iloc cannot enlarge its target object"

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?

2 Answers

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.

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])

If you’re enlarging a dataframe in a loop, see this benchmark to compare the runtime of these methods.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like