Python中基于各种条件的If else逻辑

zzoitvuj  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(131)

我需要小组的帮助,我试图根据以下条件进行逻辑:
1.有4个输入列- l1、l2、l3,两个输出列(output_瓦尔和output_col)由输入列驱动
1.如果mon的值小于5:Output_val从l1取值,而output_col将为“l1”。如果l1为nan,则优先考虑l3,然后是l2。
1.从mon 5到7(包括5和7):从l1到l2的值转换:

1. In mon 5: output_val: 0.60*l1 + 0.40*l2; output_col: 0.60*l1+0.40*l2

    2. In mon 6:  output_val: 0.50*l1 + 0.50*l2 and output_col: '0.50(l1+l2)' (If any one of them is Nan then 100% is given to the available one like in this case l2 is Nan then 1*l1);                                                                 
    3. In mon 7: output_val: 0.40*l1 + 0.60*l2 and output_col: '0.40*l1+0.60*l2'

字符串
1.从mon 8-10:output_瓦尔=l2,且output_col =“l2”,但如果l2是Nan,则优先考虑l1,然后优先考虑l3
1.从mon 11-13:output_瓦尔从l2转换到l3(与mon 5到7的逻辑类似)

1. in mon 11: output_val: 0.60*l2 + 0.40*l3; output_col: 0.60*l2+0.40*l3
    2. In mon 12:  output_val: 0.50*l2 + 0.50*l3 and output_col: '0.50(l2+l3)' (If any one of them is Nan then 100% is given to the avaiable one); 
    3. In mon 13: output_val: 0.40*l2 + 0.60*l3 and output_col: '0.40*l2 + 0.60*l3'


1.从mon 14到n:output_瓦尔:l3,且output_col=l3;如果L3不可用,则优先考虑l2,如果l1和l2不可用,则优先考虑l1
如果所有l1、l2和l3列均为Nan,则output_瓦尔=0且output_col=“”
输入框如下所示:

import numpy as np
import pandas as pd

data = {'l1': [2,3,np.nan,3,4,1,23,5,np.nan, 100, 101, 200, 121, 431, 341],
        'l2': [12,13,np.nan,13,14,np.nan,123,15,np.nan, 200, 87, 65, 23, 54, np.nan],
        'l3': [np.nan,333,111,np.nan,334,111,123,5,np.nan, np.nan, 65, 154, 341, np.nan, np.nan],
        'mon':[1,2,3,4,5,6,7,8,9,10, 11, 12, 13, 14, 15]}
data = pd.DataFrame(data)
data


带有两个额外列的输出框架:“output_瓦尔”和“output_col”如下所示:

output_data = {'l1': [2,3,1,3,4,1,23,5,np.nan, 100, 101, 200, 121, 431, 341],
        'l2': [12,13,np.nan,13,14,np.nan,123,15,np.nan, 200, 87, 65, 23, 54, np.nan],
        'l3': [np.nan,333,111,np.nan,334,111,123,5,np.nan, np.nan, 65, 154, 341, np.nan, np.nan],
        'mon':[1,2,3,4,5,6,7,8,9,10, 11, 12, 13, 14, 15],
       'output_val': [2,3,111, 3, 13.44, 1, 678.96, 15, 0, 200, 1357, 2502, 1882, 54, 341],
       'output_col':['l1', 'l1', 'l3', 'l1', '0.60*l1+0.40*l2', '1*l1', '0.40*l1+0.60*l2', 'l2', ' ', 'l2', '0.60*l2+0.40*l3', '0.50*(l2+l3)', '0.40*l2+0.60*l3', 'l2', 'l1']}

output_data = pd.DataFrame(output_data)


x1c 0d1x的数据

uajslkp6

uajslkp61#

Pandas允许写复杂的自定义项.在跳转到代码之前,不确定输出数据是否错误。

  • 索引2:输入和输出之间的l1不同。data = {'l1': [2,3,np.nan但在输出output_data = {'l1': [2,3,1
  • 索引4,根据公式输出不正确,0.6*4 + 0.4*14.0 = 8而不是13.44
  • 索引6,与上面类似,23*0.4+123*0.6=83.0而不是678.96
  • 索引10-12,与上面类似,例如,87*0.6+65*0.4 = 78.2而不是1357

下面的例子应用了上面提到的校正。

import numpy as np
import pandas as pd

def calculate_output(row):
    mon = row['mon']
    l1 = row['l1']
    l2 = row['l2']
    l3 = row['l3']

    if pd.isna(l1) and pd.isna(l2) and pd.isna(l3):
        return pd.Series({'output_val': 0, 'output_col': ' '})

    if mon < 5:
        if pd.notna(l1):
            return pd.Series({'output_val': l1, 'output_col': 'l1'})
        elif pd.notna(l3):
            return pd.Series({'output_val': l3, 'output_col': 'l3'})
        else:
            return pd.Series({'output_val': l2, 'output_col': 'l2'})

    if 5 <= mon <=7:
        if pd.isna(l1):
            return pd.Series({'output_val': l2, 'output_col': '1*l2'})
        elif pd.isna(l2):
            return pd.Series({'output_val': l1, 'output_col': '1*l1'})
        elif mon == 5:
            output_val = 0.60 * l1 + 0.40 * l2
            output_col = '0.60*l1+0.40*l2'
            return pd.Series({'output_val': output_val, 'output_col': output_col})
        elif mon == 6:
            output_val = 0.50 * (l1+l2)
            output_col = '0.50*(l1+l2)'
            return pd.Series({'output_val': output_val, 'output_col': output_col})
        elif mon == 7:
            output_val = 0.40 * l1 + 0.60 * l2
            output_col = '0.40*l1+0.60*l2'
            return pd.Series({'output_val': output_val, 'output_col': output_col})

    elif 8 <= mon <= 10:
        if pd.notna(l2):
            return pd.Series({'output_val': l2, 'output_col': 'l2'})
        elif pd.notna(l1):
            return pd.Series({'output_val': l1, 'output_col': 'l1'})
        elif pd.notna(l3):
            return pd.Series({'output_val': l3, 'output_col': 'l3'})
        else:
            return pd.Series({'output_val': 0, 'output_col': ' '})
    if 11 <= mon <= 13:
        if pd.isna(l2):
            return pd.Series({'output_val': l3, 'output_col': '1*l3'})
        elif pd.isna(l3):
            return pd.Series({'output_val': l2, 'output_col': '1*l2'})        
        elif mon == 11:
            output_val = 0.60 * l2 + 0.40 * l3
            output_col = '0.60*l2+0.40*l3'
            return pd.Series({'output_val': output_val, 'output_col': output_col})
        elif mon == 12:
            output_val = 0.50 * (l2+l3)
            output_col = '0.50*(l2+l3)'
            return pd.Series({'output_val': output_val, 'output_col': output_col})
        elif mon == 13:
            output_val = 0.40 * l2 + 0.60 * l3
            output_col = '0.40*l2+0.60*l3'
            return pd.Series({'output_val': output_val, 'output_col': output_col})
    else:
        if pd.notna(l3):
            return pd.Series({'output_val': l3, 'output_col': 'l3'})
        elif pd.notna(l2):
            return pd.Series({'output_val': l2, 'output_col': 'l2'})
        elif pd.notna(l1):
            return pd.Series({'output_val': l1, 'output_col': 'l1'})
        else:
            return pd.Series({'output_val': 0, 'output_col': ' '})

字符串
调用UDF

import numpy as np
import pandas as pd

data = {
    "l1": [2, 3, np.nan, 3, 4, 1, 23, 5, np.nan, 100, 101, 200, 121, 431, 341],
    "l2": [12, 13, np.nan, 13, 14, np.nan, 123, 15, np.nan, 200, 87, 65, 23, 54, np.nan],
    "l3": [np.nan,333,111,np.nan,334,111,123,5,np.nan,np.nan,65,154,341,np.nan,np.nan,
    ],
    "mon": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
}
data = pd.DataFrame(data)
output_df = data.apply(calculate_output, axis=1)
result_df = pd.concat([data, output_df], axis=1)


验证

from pandas.testing import assert_frame_equal

output_data = {'l1': [2,3,np.nan,3,4,1,23,5,np.nan, 100, 101, 200, 121, 431, 341],
       'l2': [12,13,np.nan,13,14,np.nan,123,15,np.nan, 200, 87, 65, 23, 54, np.nan],
       'l3': [np.nan,333,111,np.nan,334,111,123,5,np.nan, np.nan, 65, 154, 341, np.nan, np.nan],
       'mon':[1,2,3,4,5,6,7,8,9,10, 11, 12, 13, 14, 15],
       'output_val': [2,3,111, 3, 8, 1, 83.0, 15, 0, 200, 78.2, 109.5, 213.8, 54, 341],
       'output_col':['l1', 'l1', 'l3', 'l1', '0.60*l1+0.40*l2', '1*l1', '0.40*l1+0.60*l2', 'l2', ' ', 'l2', '0.60*l2+0.40*l3', '0.50*(l2+l3)', '0.40*l2+0.60*l3', 'l2', 'l1']}

output_data = pd.DataFrame(output_data)

assert_frame_equal(result_df, output_data)

相关问题