-1

The code below has to update test_df dataframe, which is currently filled with NaNs.

Each 'dig' (which is always an integer) value has corresponding 'top', 'bottom', 'left' and 'right' values, and the slices of dataframe, corresponding to respective top:bottom, left:right ranges for each 'dig', need to be updated with 'dig' values.

For example, if dig=9, top=2, botton=4, left=1 and right=5, all the NaNs within the range of 2:4, 1:5 need to be replaced with 9s.

The following code reports no errors, however, no NaNs are being updated.

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
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on how to ask a good question may also be useful. – yatu Sep 23 '19 at 09:24
  • As @yatu said, we would need to see how `letters_df` and `test_df` look like for a better understanding. – Aryerez Sep 23 '19 at 09:39
  • Thanks @Aryerez, I've added heads of both dfs. – Ian Shulman Sep 23 '19 at 09:54

1 Answers1

0

The problem I see is that in letters_df the value in column 4 is higher than the value in column 2. That means that when you do top = int(height) - int(row[2]) bottom = int(height) - int(row[4]) the value you will get in top will be bigger than the value you will get in bottem. So when you index .iloc[top:bottom] you have no rows in the slice. Maybe you should switch between top and bottem.

Aryerez
  • 3,417
  • 2
  • 9
  • 17