gogoWebsite

About "iloc cannot enlarge its target object" (the pit between iloc and loc)

Updated to 16 days ago

This is the problem code, and the report "iloc cannot enlarge its target object", which means that iloc has crossed the boundary.

# Remove characters with length one unless Chinese
for index, row in pos.iterrows():
    temp = []
    for i in row['Follow participle'].split(' '):
        if '\u4e00' <= i <= '\u9fff' and len(i) > 1:
            temp.append(i)
    pos.iloc[index, 4] = " ".join(temp)

In fact, the reason is very simple. I will first distinguish between iloc and loc. I don’t feel that I have emphasized the following points when writing these two articles:

import pandas as pd

data = [[1, 10, 'A'],
        [2, 20, 'B'],
        [3, 30, 'C']]

df = pd.DataFrame(data, columns=['month', 'price', 'level'])

print(df)
#   month  price level
#0      1     10     A
#1      2     20     B
#2      3     30     C

print(df.iloc[1])
#month     2
#price    20
#level     B

print(df.loc[1])
#month     2
#price    20
#level     B

df = df.drop(1)  # Delete rows with index 1

print(df)
#   month  price level
#0      1     10     A
#2      3     30     C

print(df.iloc[1])
#month     3
#price    30
#level     C

print(df.loc[1])
# Report an error

print(df.loc[2])
#month     3
#price    30
#level     C

Through the above small experiment, we can find that iloc[index], this index represents the real index line; loc[index], this index represents the index line marked index.
So I changed the code above to the following so that it can run correctly. It should be noted that numbers cannot be written in the loc column, but column names must be written. This also shows again: iloc[row, line], rows and columns are all real positions; loc[row, line], the rows and columns here can be understood as str, and loc is the place corresponding to the same characters as row and line.

pos.loc[index, 'Follow participle'] = " ".join(temp)