简体   繁体   English

动态添加列并从另一列分配计算值

[英]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.

相关问题 pyspark - 根据另一个计算列的计算值更新列 - pyspark - Updating a column based on a calculated value from another calculated column 在另一列中搜索列值并将找到的行的下一列中的值分配给另一列 - Search for column values in another column and assign a value from the next column from the row found to another column 用从数据框中另一列的最大值计算的值替换字符串 - Replacing string with value calculated from the max of another column in a dataframe 添加从现有列计算的ASCII列 - Add column in ASCII that is calculated from existing column 从计算值中排除列 - Exclude a column from calculated value 使用先前计算的值(来自同一列)和Pandas Dataframe中另一列的值来计算值 - Calculate value using previously-calculated value (from the same column) and value from another column in a Pandas Dataframe 如何在数据框中添加具有计算值的另一列 - How to add another column in dataframe with calculated values 根据另一列中的字符串在列中分配值 - Assign value in column based on string in another column 根据另一个列中的值在数组中添加列的编号 - Add numbers of a column within an array based of value from another column 从 column1 动态获取值并将其分配给同一列中的 NaN 值,如果它们都具有相同的 column2 值 - Dynamically get value from column1 and assign it to NaN values in same column, if they all have the same column2 value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM