0

I encounter a strange problem in pretty big piece of code. Normally, I use .loc to change particular items in a certain column within a loop while using a row_index variable as help. Lets assume to have the following:

df['Column1'] = 0

list = [0,1,2,3,4,...,100]
for x in list
    ....       
    print senti_pos_value
    print output_rowindex_list        

    df.iloc[output_rowindex_list,df.columns.get_loc('Column1')] = senti_pos_value

    output_rowindex_list = output_rowindex_list + 1 

The print commands within the loop give me (for the first 6 iterations) something like:

    24
    0

    22
    1

    24
    2

    27
    3

    113
    4

    4
    5

senti_pos_value and output_rowindex_list both are integer values. The latter is strictly increasing by one for every iteration within the loop.

senti_pos_value itself changes arbitrarily based on a number of further complex operations (~400 lines of code). However, the final result is always an integer.

So I want to write all senti_pos_values - row-by-row - in the same column. I haven't had any problem with such issues so far, but finally it turns out that the code does not work. It simply doesn't write almost anything at all and the numbers just remain zero for Column1 (c.f. below).

Also i tried the following:

 df.loc[output_rowindex_list,'Column1'] = senti_pos_value

and:

 df.set_value(output_rowindex_list,'Column1',senti_pos_value)

No success either... for both I receive, i.e. for one particular outline within the loop: output_rowindex_list = 113 and senti_pos_value = 4

  TypeError: cannot do index indexing on <class 'pandas.tseries.index.DatetimeIndex'> with these indexers [113] of <type 'int'>

As stated above: df.iloc[output_rowindex_list,df.columns.get_loc('Column1')]=senti_pos_value

does not return any error when setting a breakpoint and entering the relevant line of code manually, but the final dataframe unfortunately looks like this for my column (referring to the same inputs as shown by print-commmand):

    Column1
    4
    0
    0
    0
    0
    0

Also, please note that no matter which command I used while not setting a break point at all, the code always works without breaking up. In this case, the result is always as stated above for Column 1.

I am not so new to pandas but it took my hours to figure it out and I simply can't see the reason... Any help is highly appreciated!

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Why are you doing it in a loop? That is just horror! You need to figure out how to do your task without loops. That will make it faster, cleaner and better to debug and modify columns. Please rephrase your question to include a MCVE: http://stackoverflow.com/help/mcve – Kartik Aug 06 '16 at 19:05
  • There is no way around this as this operation is part of a much larger program including thousand lines of code. The example as given is appicable anyway. – Ripple10247 Aug 06 '16 at 19:23
  • Besides, do you have any helpful input on this? - i.e. what the error actually tells? – Ripple10247 Aug 06 '16 at 19:23

0 Answers0