python - In a pandas dataframe, how do you add a value to the end of each row in iterrows? -


say have simple dataframe products, current prices , average prices. want use current , average prices calculate retail price. attempt is:

import csv pandas import series, dataframe import pandas pd import os  frame = pd.read_csv('/ ... /data.csv')  index, row in frame.iterrows():       product = row['product']   price = row['price']       ave_price = row['ave_price']   weight_price = 2.0   max_price = ave_price * weight_price    retail_price = max_price / (1.0 + 2.0 * price / ave+price)   retail_total = rs_price * 1.0875  frame.to_csv('/users ... /output.csv', encoding ='utf-8') 

how retail_total , add in such way can print entire dataframe products, current prices, average prices , retail prices?

when try it, populates of products' retail price last 1 in list of products:

add column frame:

frame['retail_price'] = series(np.zeros(len(frame)), index=frame.index) 

then store value each row inside loop

for index, row in frame.iterrows():       product = row['product']   price = row['price']       ave_price = row['ave_price']   weight_price = 2.0   max_price = ave_price * weight_price    retail_price = max_price / (1.0 + 2.0 * price / ave_price)   retail_total = retail_price * 1.0875    row['retail_price'] = retail_price 

Comments