[英]panda data frame applying multiple columns
product_code order eachprice
TN45 10 500
BY11 20 360
AJ21 5 800
and i need to create a new column based on order and each price if order>=10, then 5% discount, order>=50 then 10% discount for the price, how can i apply a function to achieve this:我需要根据订单和每个价格创建一个新列,如果订单>=10,则折扣 5%,订单>=50,然后价格折扣 10%,我如何申请 function 来实现此目的:
product_code order each_price discounted_price
TN45 10 500 4500
BY11 20 360 6480
AJ21 5 800 4000
i tried to apply a function eg df['discount'] = df.apply(function, axis=1)我尝试应用 function 例如 df['discount'] = df.apply(function, axis=1)
but errors prompts "A value is trying to be set on a copy of a slice from a DataFrame. Try using.loc[row_indexer,col_indexer] = value instead"但错误提示“试图在 DataFrame 的切片副本上设置一个值。尝试使用 .loc[row_indexer,col_indexer] = 值代替”
can anyone help?谁能帮忙? thanks谢谢
You could use nested numpy.where
calls to achieve this.您可以使用嵌套numpy.where
调用来实现此目的。 I've added an extra intermediate column to the results for the percentage discount, then used this column to calculate the final discounted price:我在百分比折扣的结果中添加了一个额外的中间列,然后使用此列计算最终折扣价:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'product_code': ['TN45', 'BY11', 'AJ21'],
'order': [10, 20, 5],
'each_price': [500, 360, 800]
})
df['discount'] = np.where(
df['order'] >= 50,
0.1,
np.where(
df['order'] >= 10,
0.05,
0
)
)
df['discounted_price'] = df['order'] * df['each_price'] * (1 - df['discount'])
Note that my results are slightly different from those in your expected output, but I believe they are correct based on the description of the discount conditions you gave:请注意,我的结果与您预期的 output 中的结果略有不同,但根据您对折扣条件的描述,我相信它们是正确的:
product_code order each_price discount discounted_price
0 TN45 10 500 0.05 4750.0
1 BY11 20 360 0.05 6840.0
2 AJ21 5 800 0.00 4000.0
As you mention you are trying by using apply function. I did the same and is working.正如您提到的,您正在尝试使用 apply function。我做了同样的事情并且正在工作。 I am not sure what part of the function was wrong in your case.我不确定您的情况下 function 的哪一部分是错误的。
import pandas as pd
df = pd.DataFrame({
'product_code': ['TN45', 'BY11', 'AJ21'],
'order': [10, 20, 5],
'each_price': [500, 360, 800]
})
# This is the apply function
def make_discount(row):
total=row["order"] * row['each_price']
if row["order"] >= 10:
total=total - (total*0.05)
elif row["order"] >= 50:
total=total - (total*0.1)
return total
df["discount_price"] = df.apply(make_discount, axis=1)
df
product_code order each_price discount_price
0 TN45 10 500 4750.0
1 BY11 20 360 6840.0
2 AJ21 5 800 4000.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.