mercredi 28 octobre 2020

Create variable based on two other variables

In Python, I would like to create a new variable c based on the value of a and b.

if a in ('GBP', 'AUD', 'CNY', 'NZD'):
    if b == '[00Y, 01Y]':
        c= '90'
    elif b == '[01Y, 02Y]':
        c = '85'
    elif b == '[02Y, 03Y]':
        c = '80'
    elif b == '[03Y, 04Y]':
        c = '75'
    elif b == '[04Y, 05Y]':
        c = '70'
elif a in ('EUR', 'USD', 'CHF', 'CAD', 'SGD', 'HKD', 'JPY'):
    if b == '[00Y, 01Y]':
        c = '95'
    elif b == '[01Y, 02Y]':
        c = '90'
    elif b == '[02Y, 03Y]':
        c = '85'
    elif b == '[03Y, 04Y]':
        c = '80'
    elif b == '[04Y, 05Y]':
        c = '75'
    elif b == '[05Y, 07Y]':
        c = '60'
    elif b == '[07Y, 10Y]':
        c = '55'

a and b are columns of a dataframe and I have to use apply to finally obtain what I desire.

Although this perfectly works, I think it is long code for such a small operation and I wonder if there is a more elegant way to do the same. I know the np.select condition but it forces me to repeat the condition on `a, which I find is not elegant either.

Thanks,

Aucun commentaire:

Enregistrer un commentaire