Mika
Mika

Reputation: 85

Adding new column by multiple conditions in multiple columns

I have a dataframe with columns currency, loan_amnt. And variables with currency values for each month. I have to check if currency equal to usd, then divide loan_amnt to usd currency value and save in new column. Same for eur and jpy. But if currency is uzs, then pass it. I wrote function but it didn't work.

sample df:

currency    loan_amnt
0   usd 100000
1   eur 150000
2   jpy 85000
3   uzs 90000
4   usd 50000
5   eur 65000

variables:

feb_usd =   9563,33
feb_eur = 10541,66
feb_jpy = 87,52

my function:

def currency_diff(data, usd, eur, jpy):
    for i, row in data.iterrows():
        currency= row['currency']
        loan_amnt= row['loan_amnt']
        if currency== 'USD':
            return loan_amnt/ usd
        elif currency== 'EUR':
            return loan_amnt/ eur
        elif currency== 'JPY':
            return loan_amnt/ jpy
        else:
            loan_amnt


df['nominal'] = df.apply(lambda x: currency_diff(x, feb_usd, feb_eur, feb_jpy))

It didnt work. Any help would be much appreciated! Thank you.

Upvotes: 0

Views: 76

Answers (2)

Umar.H
Umar.H

Reputation: 23099

You could tackle this a number of ways, in regards to your function your invoking a loop, then invoking another loop which is bad and most likely causing it to fail. You could use np.select like so.

feb_usd =   9563.33
feb_eur = 10541.66
feb_jpy = 87.52


cur = df['currency']
loan = df['loan_amnt']


conditions = [cur == "usd", cur == "eur", cur == "jpy"]

outcomes = [loan / feb_usd, loan / feb_eur, loan / feb_jpy]


df['loan'] = np.select(conditions,outcomes ,default=loan)

a much simpler solution would be to use .map

currency_dict = {'usd' : 9563.33,
                'eur' : 10541.66,
                'jpy' : 87.52}

(df['loan_amnt'] / df['currency'].map(currency_dict)).fillna(df['loan_amnt'])

0       10.456609
1       14.229258
2      971.206581
3    90000.000000
4        5.228304
5        6.166012
dtype: float64

Upvotes: 1

Fred
Fred

Reputation: 502

The apply function works on a row by row basis, so that makes the data.iterrows() portion of your code invalid.

Next even if you passed a DataFrame into the function, on the first iteration you return loan_amnt / currency so your function will always only return a single value no matter what the DataFrame size.

Perhaps a better solution:

currency_mapping = {
    "usd" : 9563.33,
    "eur" : 10541.66,
    "jpy" : 87.52,
}

df["conversion"] = df["currency"].map(currency_mapping)

df["nominal"] = df["loan_amnt"]/df["conversion"]

Upvotes: 1

Related Questions