假设我有一个简单的数据框,包含产品,当前价格和平均价格。我想用当前和平均价格来计算零售价格。我的尝试是:
import csv
from pandas import Series, DataFrame
import pandas as pd
import os
frame = pd.read_csv('/ ... /data.csv')
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 = rs_price * 1.0875
frame.to_csv('/Users ... /output.csv', encoding ='utf-8')
如何获取retail_total并以这样的方式添加它以便我可以使用产品,当前价格,平均价格和零售价格打印整个数据框?
当我尝试这样做时,它只会填充所有产品'零售价格作为产品清单中的最后一个:
答案 0 :(得分:3)
在框架中添加一列:
frame['retail_price'] = Series(np.zeros(len(frame)), index=frame.index)
然后存储for循环中每行的值
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
答案 1 :(得分:2)
import pandas as pd
import numpy as np
# simulate some artificial data
# ===========================================
np.random.seed(0)
product = np.random.choice(list('ABCDE'), size=10)
price = np.random.randint(100, 200, size=10)
avg_price = np.random.randint(100, 200, size=10)
df = pd.DataFrame(dict(product=product, price=price, avg_price=avg_price))
df
avg_price price product
0 125 188 E
1 177 112 A
2 172 158 D
3 109 165 D
4 120 139 D
5 180 187 B
6 169 146 D
7 179 188 C
8 147 181 E
9 164 137 A
# processing
# ===========================================
# some constant parameters
weight_price = 2.0
df['retail_price'] = df['avg_price'] * weight_price / (1.0 + 2.0 * df['price'] / df['avg_price'])
df['retail_total'] = df['retail_price'] * 1.0875
df
avg_price price product retail_price retail_total
0 125 188 E 62.3752 67.8331
1 177 112 A 156.2544 169.9266
2 172 158 D 121.2459 131.8549
3 109 165 D 54.1276 58.8637
4 120 139 D 72.3618 78.6935
5 180 187 B 116.9675 127.2022
6 169 146 D 123.9089 134.7509
7 179 188 C 115.4631 125.5661
8 147 181 E 84.9077 92.3371
9 164 137 A 122.8128 133.5589
# df.to_csv()