下面的代码必须更新test_df dataframe
,目前已填充NaN
。
每个“ dig”(始终为整数)值具有对应的“ top”,“ bottom”,“ left”和“ right”值,以及对应于各自的top:bottom,left:right的数据帧切片每个“摘要”的范围都需要使用“摘要”值进行更新。
例如,如果dig = 9,top = 2,botton = 4,left = 1和right = 5,则需要替换2:4、1:5范围内的所有NaN
9s。
以下代码未报告任何错误,但是,没有更新NaN。
for index, row in letters_df.iterrows():
dig = str(row[0])
top = int(height) - int(row[2])
bottom = int(height) - int(row[4])
left = int(row[1])
right = int(row[3])
test_df.iloc[top:bottom, left:right] = dig
test_df:
0 1 2 3 4 5 6 ... 633 634 635 636 637 638 639
0 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN
4 NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN
letters_df:
0 1 2 3 4 5 dig_unique_letters
0 T 36 364 51 388 0 0
1 h 36 364 55 388 0 1
2 i 57 364 71 388 0 2
3 s 76 364 96 388 0 3
4 i 109 364 112 388 0 2
答案 0 :(得分:0)
我看到的问题是,在letters_df
中,第4列中的值高于第2列中的值。这意味着,当您执行top = int(height) - int(row[2])
bottom = int(height) - int(row[4])
时,将在{{1}中得到}将大于您在top
中获得的值。因此,在索引bottem
时,切片中没有行。也许您应该在.iloc[top:bottom]
和top
之间切换。