下面是一个Python函数示例,用于将满减优惠转换为折扣率形式,并创建包含Coupon_id和Discount的表”table1”来保存优惠券折扣率:
import pandas as pd
def convert_discount(discount):
if ':' in discount:
full_price, minus = map(int, discount.split(':'))
return round(1 - (minus / full_price), 2)
else:
return float(discount)
# 假设你已经有了一个名为df的数据框,其中包含了原始的Discount_rate列
# 创建新的DataFrame table1,包含Coupon_id和Discount两列
table1 = pd.DataFrame(columns=['Coupon_id', 'Discount'])
# 假设Coupon_id存储在叫做Coupon_id的列中,Discount_rate存储在叫做Discount_rate的列中
for index, row in df.iterrows():
coupon_id = row['Coupon_id']
discount_rate = row['Discount_rate']
converted_discount = convert_discount(discount_rate)
table1.loc[index] = [coupon_id, converted_discount]
# 输出结果
print(table1)
请注意,这只是一个示例代码,你需要根据实际情况调整变量名称和数据框对象。
内容由零声教学AI助手提供,问题来源于学员提问