mardi 10 avril 2018

Python pandas if statement based off of boolean qualifier

I am try to do an IF statement where it keeps my currency pairs in alphabetic ordering (i.e. USD/EUR would flip to EUR/USD because E alphabetically comes before U, however CHF/JPY would stay the same because C comes alphabetically before J.) Initially I was going to write code specific to that, but realized there were other fields I'd need to flip (mainly changing a sign for positive to negative or vice versa.)

So what I did was write a function to create a new column and make a boolean identifier as to whether or not the field needs action (True) or not (False).

def flipFx(ccypair):
    first = ccypair[:3]
    last = ccypair[-3:]
    if(first > last):
        return True
    else:
        return False

brsPosFwd['Flip?'] = brsPosFwd['Currency Pair'].apply(flipFx)    

This works great and does what I want it to.

Then I try and write an IF statement to use that field to create two new columns:

if brsPosFwd['Flip?'] is True:
    brsPosFwd['CurrencyFlip'] = brsPosFwd['Sec Desc'].apply(lambda x: 
x.str[-3:]+"/"+x.str[:3])
    brsPosFwd['NotionalFlip'] = -brsPosFwd['Current Face']
else:
    brsPosFwd['CurrencyFlip'] = brsPosFwd['Sec Desc']
    brsPosFwd['NotionalFlip'] = brsPosFwd['Current Face']

However, this is not working properly. It's creating the two new fields, CurrencyFlip and NotionalFlip but treating every record like it is False and just pasting what came before it.

Does anyone have any ideas? I am somewhat new to Pandas.

Aucun commentaire:

Enregistrer un commentaire