简体   繁体   English

Python根据条件将值转移到列

[英]Python Shifting a Value to Columns Based on a Condition

I am facing a problem related to a delivery offset in the example dataset.我面临与示例数据集中的交付偏移相关的问题。

df = pd.DataFrame({'Platform ID' : [1,2,3,4], "2018" : [0,0,0,0],
                                              "2019" : [0,0,0,0], 
                                              "2020" : [21,3,8,23], 
                                              "2021" : [31,0,10,12], 
                                              "2022" : [2,23,13,14],
                   'Offsets (in months)' : [6, 12, 18, 0]})
df

What I need is a time shift based on the last column if the delivery is 6 months the half of the value should go to the previous year's column.我需要的是基于最后一列的时间偏移,如果交付时间为 6 个月,那么价值的一半应该转到上一年的列。 If the time shift is 12 months the whole value must move to the previous year's column.如果时移是 12 个月,则整个值必须移动到上一年的列。 If the time shift is 18 the value must move to the previous column and one-half of it must move even further like by 6 months in the first case.如果时间偏移为 18,则该值必须移动到前一列,并且其中的一半必须进一步移动,就像第一种情况下的 6 个月一样。

I tried to use shift() however it moves the whole column.我尝试使用 shift() 但是它移动了整个列。 Is there a way how to set this condition in this or another function?有没有办法在这个或另一个函数中设置这个条件?

Desired output:期望的输出:

df = pd.DataFrame({'Platform ID' : [1,2,3,4], "2018" : [0,0,4,0],
                                              "2019" : [10.5,3,9,0], 
                                              "2020" : [26,0,11.5,23], 
                                              "2021" : [16.5,23,6.5,12], 
                                              "2022" : [1,0,0,14],
                   'Offsets (in months)' : [6, 12, 18, 0]})
df

在此处输入图片说明

This is in no way the most elegant solution out there, but given that each row essentially follows a different functional shift, I can't think of a much neater way of doing it than the below.这绝不是最优雅的解决方案,但考虑到每一行本质上都遵循不同的功能转变,我想不出比下面更简洁的方法。 I have amended my previous answer to adjust it to the new provided information.我已经修改了我之前的答案,以将其调整为新提供的信息。 If a neater answer exists, I would be curious to see it.如果存在更简洁的答案,我会很想看到它。

df = pd.DataFrame({'Platform ID' : [1,2,3,4], "2018" : [0,0,0,0],
                                              "2019" : [0,0,0,0], 
                                              "2020" : [21,3,8,23], 
                                              "2021" : [31,0,10,12], 
                                              "2022" : [2,23,13,14],
                   'Offsets (in months)' : [6, 12, 18, 0]})
for i in df['Offsets (in months)'].unique():
    if i == 6:
        df_6 = df[df['Offsets (in months)'] == 6]
        df_6.iloc[:,1:6] = (df_6.iloc[:,1:6]/2).shift(periods=-1, fill_value=0, axis=1) +df_6.iloc[:,1:6]/2
        df[df['Offsets (in months)'] == 6] = df_6
    elif i == 12:
        df_12 = df[df['Offsets (in months)'] == 12]
        df_12.iloc[:,1:6] = df_12.iloc[:,1:6].shift(periods=-1, fill_value=0, axis=1)
        df[df['Offsets (in months)'] == 12] = df_12
    elif i == 18:
        df_18 = df[df['Offsets (in months)'] == 18]
        df_18.iloc[:,1:6] = df_18.iloc[:,1:6].shift(periods=-1, fill_value=0, axis=1)
        df_18.iloc[:,1:6] = (df_18.iloc[:,1:6]/2).shift(periods=-1, fill_value=0, axis=1) +df_18.iloc[:,1:6]/2
        df[df['Offsets (in months)'] == 18] = df_18

Unless there is a typo, this should do the trick.除非有错别字,否则这应该可以解决问题。 I am rushed so I might have made a mistake.我很匆忙,所以我可能犯了一个错误。 Basically, the idea is to filter out each of the offsets using boolean indexing and then shifting the values excluding Platform ID and Offset (by specifying iloc[1:6].基本上,这个想法是使用布尔索引过滤掉每个偏移量,然后移动不包括平台 ID 和偏移量的值(通过指定 iloc[1:6].

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM