0

I'm iterating over a dataframe and I want to add new elements to each row, so that I can add the new row to a second dataframe.

for index, row in df1.iterrows():
    # I looking for somethis like this:
    new_row = row.append({'column_name_A':10})

    df2 = df2.append(new_row,ignore_index=True)
3
  • Before for loop: new_row =[] in For loop: new_row.append({'column_name_A':10}) Commented Sep 18, 2018 at 9:36
  • why not create a full column with df['column_name_A''] = 10 and not itterate over the rows? Commented Sep 18, 2018 at 10:04
  • This is just an example. In each iteration, I will add different values. But thanks. Commented Sep 18, 2018 at 10:06

1 Answer 1

1

If I understand correctly, you want to have a copy of your original dataframe with a new column added. You can create a copy of the original dataframe, add the new column to it and then iterate over the rows of the new dataframe to update the values of the new column as you would have done in your code posted in the question.

df2 = df1.copy()
df2['column_name_A'] = 0
for index, row in df2.iterrows():
    row['column_name_A'] = some_value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.