Compute a new column elementwise by looping over two parallel value lists and appending each product. With pandas, a single vectorized expression df['price'] * df['qty'] produces all products at once, and df.assign attaches the result as a new column without mutating the original DataFrame.

By hand

Loop over each index i, multiply price[i] by qty[i], and append the product to total. After the loop, total holds the new derived column.

naive.py
Replay: real traced execution (multi-file project)
price = [10, 25, 5, 20]
qty = [3, 2, 8, 1]
total = []
for i in range(len(price)):
    total.append(price[i] * qty[i])
print('RESULT:', total)
  1. price ← [10, 25, 5, 20]

    1price = [10, 25, 5, 20]2qty = [3, 2, 8, 1]
    values this step[10, 25, 5, 20]price
  2. qty ← [3, 2, 8, 1]

    1price = [10, 25, 5, 20]2qty = [3, 2, 8, 1]3total = []
    values this step[3, 2, 8, 1]qty
  3. total ← []

    2qty = [3, 2, 8, 1]3total = []4for i in range(len(price)):
    values this step[]total
  4. i ← 0

    3total = []4for i in range(len(price)):5    total.append(price[i] * qty[i])
    values this step0i
  5. total ← [30]

    4for i in range(len(price)):5    total.append(price[i] * qty[i])6print('RESULT:', total)
    values this step[] [30]total
  6. i ← 1

    3total = []4for i in range(len(price)):5    total.append(price[i] * qty[i])
    values this step0 1i
  7. total ← [30, 50]

    4for i in range(len(price)):5    total.append(price[i] * qty[i])6print('RESULT:', total)
    values this step[30] [30, 50]total
  8. i ← 2

    3total = []4for i in range(len(price)):5    total.append(price[i] * qty[i])
    values this step1 2i
  9. total ← [30, 50, 40]

    4for i in range(len(price)):5    total.append(price[i] * qty[i])6print('RESULT:', total)
    values this step[30, 50] [30, 50, 40]total
  10. i ← 3

    3total = []4for i in range(len(price)):5    total.append(price[i] * qty[i])
    values this step2 3i
  11. total ← [30, 50, 40, 20]

    4for i in range(len(price)):5    total.append(price[i] * qty[i])6print('RESULT:', total)
    values this step[30, 50, 40] [30, 50, 40, 20]total
  12. for i in range(len(price)):

    3total = []4for i in range(len(price)):5    total.append(price[i] * qty[i])
  13. stdout ← RESULT: [30, 50, 40, 20]

    5    total.append(price[i] * qty[i])6print('RESULT:', total)
    values this stepRESULT: [30, 50, 40, 20]stdout

With pandas

df.assign(total=df['price'] * df['qty']) multiplies the two Series elementwise (index-aligned) and returns a new DataFrame with the total column appended. The snapshot shows the input columns, then the new column as a Series.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

price = [10, 25, 5, 20]
qty = [3, 2, 8, 1]
df = pd.DataFrame({'price': price, 'qty': qty})
df2 = df.assign(total=df['price'] * df['qty'])
col = df2['total']
print('price:', df['price'].tolist())
print('qty:', df['qty'].tolist())
print('index:', col.index.tolist())
print('values:', col.tolist())
print('dtype:', col.dtype)
print('RESULT:', col.tolist())
price: [10, 25, 5, 20]
qty: [3, 2, 8, 1]
index: [0, 1, 2, 3]
values: [30, 50, 40, 20]
dtype: int64
RESULT: [30, 50, 40, 20]

Implementation notes

  • df.assign returns a new DataFrame with the added column; the original df is unchanged. This makes it safe to chain: df.assign(...).assign(...).
  • df['price'] * df['qty'] is vectorized column arithmetic — the same broadcast-and-apply pattern as NumPy elementwise multiplication (see elementwise-product in python-numpy ch03), applied to Series aligned by index label.
  • To add the column in-place instead, use df['total'] = df['price'] * df['qty']. Prefer assign in pipelines; prefer direct assignment for one-off mutations.
  • Cross-reference: add-derived-field (python-data-basics) for the pure-Python dict version; elementwise-product (python-numpy ch03) for the NumPy array version.