0

So I am running a for loop within my code and it is not updating my dataframe. For example if I am multiplying some values in the code below and then when I try to print the head or tail it is not updated to what the new values should be. (the value started out as 135 below) Is there anyway to do this without having to make a new dataframe? Thank you so much!

for index, row in rick_sales.iterrows():
if row['Order description'] == 'Cross Connect':
    row['amount'] = row['amount'] * 9.33
else:
    row['amount'] = row['amount'] * 1.9

rick_sales.head()
salesmen  Order description amount  date

12 rick Cross Connect 135.0 2021-02-22

max2lax
  • 1
  • 1
  • 2
    When you iterate over a DataFrame with `iterrows` method, in each iteration you get a copy of the current row (not the actual row) so you are changing that copy. Use a vectorized method instead `rick_sales['amount'] = np.where(rick_sales['Order description'] == 'Cross Connect', rick_sales['amount'] * 9.33, rick_sales['amount'] * 1.9)` – ayhan Jun 02 '22 at 16:41

0 Answers0