vendredi 2 avril 2021

Loop through rows of df and perform calculations based on if-statement

I am trying to calculate my brokerage fees in several currencies into USD. So if I paid the fee in EUR, the fee should be multiplied with that days EUR/USD exchange rate.

Example dataframe:

import pandas as pd
df = pd.DataFrame({"Fee Coin": ["EUR", "BTC"],
                   "Fee": [3, 0.0005],
                    "USD Price": [1.05, 1.1],
                     "BTC Price": [24000, 27000]})

The output for the first row should be 3.15 and the second should be 13.5. The output should be displayed in a new column which I have tried calling Fee in USD.

What I have tried, which gives the wrong calculations:

for value in df["Fee Coin"]:
    if value == "EUR":
        df["Fee in USD"] = df.Fee*df["USD Price"]
    elif value == "BTC":
        df["Fee in USD"] = df.Fee*df["BTC Price"]

Aucun commentaire:

Enregistrer un commentaire