0

I am trying to have a code that does the following:

#create a new column in a dataframe 
df['new_column'] = 0

and for every row in this dataframe, it looks at whether column B's value is 1. If it is, it populates the new column with a 1, it then jumps 126 rows forward, and populates that row of the new column with a -1; otherwise, it populates the row of the new column with a 0. Once done that, the formula should continue from a row that would depend on the value encountered in the column B (i.e., if in the first iteration, column B's value=1, the row from which the loop restarts will be 127, alternatively it would be row 2)

for row in df.iterrows():
    for item in df['new_column']:
        if df.B[df.new_column[df.new_column.index[item]]] == 1:
            item = 1
            row += 126
            item = -1
        else:
            item = 0

Problem is that I get: "TypeError: can only concatenate tuple (not "int") to tuple". Could you please help me?

gianluca
  • 77
  • 1
  • 2
  • 6
  • 1
    You're looking for `df_zing.iterrows()`. – IanS Mar 19 '19 at 14:22
  • 3
    Possible duplicate of [How to iterate over rows in a DataFrame in Pandas?](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – mad_ Mar 19 '19 at 14:27
  • Does this answer your question? [pandas - AttributeError 'dataframe' object has no attribute](https://stackoverflow.com/questions/51502263/pandas-attributeerror-dataframe-object-has-no-attribute) – cottontail Feb 02 '23 at 19:20

0 Answers0