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 objectWhat 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'] = valueHere 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.