简体   繁体   English

在熊猫数据框中,如何在迭代的每一行的末尾添加一个值?

[英]In a pandas dataframe, how do you add a value to the end of each row in iterrows?

Say I have a simple dataframe with products, current prices and average prices. 假设我有一个包含产品,当前价格和平均价格的简单数据框。 I want to use the current and average prices to calculate a retail price. 我想使用当前价格和平均价格来计算零售价。 My attempt is: 我的尝试是:

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')

How do I get the retail_total and add it in such a way that I can print the entire dataframe with the products, current prices, average prices AND retail prices? 如何获得Retail_total并将其添加到可以打印产品,当前价格,平均价格和零售价格的整个数据框的方式?

When I try to do it, it just populates ALL of the products' retail price as the last one in the list of products: 当我尝试这样做时,它只是将所有产品的零售价格填充为产品列表中的最后一个:

Add a column to the frame: 在框架中添加一列:

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

Then store the value for each row inside the for loop 然后将每行的值存储在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
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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Pandas 向 Dataframe 的每一行添加值? - How do you add Values to each row of the Dataframe Using Pandas? 如何添加一个简单的计数器列,每列增加1到Pandas DataFrame? - How do you add a simple counter column that increases by one in each row to a Pandas DataFrame? 如何在熊猫的前一行中添加数组? - How do you add an array to each previous row in pandas? 如何在 dataframe 的每一行末尾添加 ****? - how to add **** at the end of each row in a dataframe? 您如何为单独的 dataframe 中的每个值重复 dataframe 的每一行,然后将两者组合成一个 dataframe? - How do you repeat each row for a dataframe for each value in a seperate dataframe and then combine the two into a single dataframe? 如何在df.iterrows()期间删除pandas数据帧中的当前行 - How to delete the current row in pandas dataframe during df.iterrows() 如何将 iterrows 的 output 行连接到具有相同列的另一个 pandas DataFrame ? - How to concat the row output of iterrows to another pandas DataFrame with the same columns? 如何避免此熊猫数据框的迭代 - How to avoid iterrows for this pandas dataframe Pandas 在每个唯一列值的末尾添加总行 - Pandas add total row at the end of each unique column value 如何在熊猫中嵌套嵌套 - How to do nested iterrows in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM