[英]add column dynamically and assign calculated value from another column
I have some prepaid sales orders and need to assign the sales value to the subsequent months according to the number of months the sales order cover (paid/month).我有一些预付销售订单,需要根据销售订单涵盖的月数(已付/月)将销售价值分配给后续月份。 the dataframe looks like this: dataframe 看起来像这样:
order number of months year start month start year paid
1 2 2021 10 2021 300
2 3 2021 10 2021 300
3 1 2021 11 2021 50
.... and it should look like ....它应该看起来像
order months year start_month start_year paid 2021_10 2021_11 2021_12
1 2 2021 10 2021 300 150 150
2 3 2021 10 2021 300 100 100 100
3 1 2021 11 2021 50 50
.... ……
Thank you!谢谢!
I am assuming you are using pandas.我假设您使用的是 pandas。
First, you will need to get all the combinations of months and years in which there were sales.首先,您需要获得销售月份和年份的所有组合。 This depends on your edge cases and data.这取决于您的边缘情况和数据。 but for the sake of the example:但为了这个例子:
date_combinations = [(10,2021), (11,2021), (12,2021)]
Now, the way to dynamically add columns and fill them based on multiple other rows is to use the 'apply' function on the entire data frame and get the rows like this:现在,动态添加列并根据多个其他行填充它们的方法是在整个数据帧上使用“应用”function 并获得如下行:
def fill_date_col(row):
if date[0] < row["start_month"] + row["months"] and date[0] >= row["start_month"] and row["start_year"] == date[1] :
return row["paid"]/row["months"]
else:
return None
for date in date_combinations:
df[f"{date[0]}_{date[1]}"] = df.apply(lambda row: fill_date_col(row), axis=1)
and this is the result这就是结果
order months start_month start_year paid 10_2021 11_2021 12_2021
0 1 2 10 2021 300 150.0 150.0 NaN
1 2 3 10 2021 300 100.0 100.0 100.0
2 3 1 11 2021 50 NaN 50.0 NaN
Of course, you need the adjust this with regard to your span of dates and edge cases, especially if some sales span over a year.当然,您需要根据日期跨度和边缘情况进行调整,特别是如果某些销售跨越一年。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.