Python: If Formula Not Working on Dataframe | ValueError: The truth value of a DataFrame is ambiguous

Viewed 49

I have a dataframe with different currencies.
I'm creating an if formula to apply to a specific column and give me the results in another column:

Code:

def CurConv(x):
    if rst[rst["Currency"]=="Botswana Pula(P)"]:
           return x*0.094
    elif rst[rst["Currency"]=="Brazilian Real(R$)"]:
           return x*0.2
    elif rst[rst["Currency"]=="Dollar($)"]:
           return x
    elif rst[rst["Currency"]=="Emirati Diram(AED)"]:
           return x*0.27
    elif rst[rst["Currency"]=="Indian Rupees(Rs.)"]:
           return x*0.014
    elif rst[rst["Currency"]=="Indonesian Rupiah(IDR)"]:
           return x*0.000070
    elif rst[rst["Currency"]=="NewZealand($)"]:
           return x*0.71
    elif rst[rst["Currency"]=="Pounds(å£)"]:
           return x*1.41
    elif rst[rst["Currency"]=="Qatari Rial(QR)"]:
           return x*0.27
    elif rst[rst["Currency"]=="Rand(R)"]:
           return x*0.073
    elif rst[rst["Currency"]=="Sri Lankan Rupee(LKR)"]:
           return x*0.0051
    elif rst[rst["Currency"]=="Turkish Lira(TL)"]:
           return x*0.12
    else:
        return "NaN"
rst["USD"]=rst.AverageCostfortwo.apply(CurConv)

But I keep getting this error:"ValueError: The truth value of a DataFrame is ambiguous"

2 Answers

If you are hoping to get True if the result is not empty, you might want to use:

 if not rst[rst["Currency"]=="Botswana Pula(P)"].empty:

However, the issue here was not the if statement, looks like (from the comments you added later to your question) you are trying to adjust the currencies to make a USD column. Here's my crack at it.

You can also do calculations by row, if you use the apply() method on the dataframe itself rather than its series components.

def CurConv(curr):
    '''Taking a row, return a value for the conversion factor'''
    if curr == "Botswana Pula(P)":
        return 0.094
    elif curr == "Brazilian Real(R$)":
        return 0.2
    elif curr == "Dollar($)":
        return 1
    elif curr == "Emirati Diram(AED)":
        return 0.27
    elif curr == "Indian Rupees(Rs.)":
        return 0.014
    elif curr == "Indonesian Rupiah(IDR)":
        return 0.000070
    elif currency == "NewZealand($)":
        return 0.71
    elif curr == "Pounds(å£)":
        return 1.41
    elif curr == "Qatari Rial(QR)":
        return 0.27
    elif curr =="Rand(R)":
        return 0.073
    elif curr == "Sri Lankan Rupee(LKR)":
        return 0.0051
    elif curr == "Turkish Lira(TL)":
        return 0.12
    else:
        return float("nan")

rst["USD"] = rst['AverageCostfortwo'] * rst['currency'].apply(CurConv)

[IMPORTANT: please also vote up Ben Y's https://stackoverflow.com/a/67994153/206413]

To elaborate on the error, let's focus in the first statement:

if rst[rst["Currency"]=="Botswana Pula(P)"]:
       return x*0.094

Here, rst["Currency"]=="Botswana Pula(P)" is a mask, returning a Series of True or False for every row if the given row has Currency == "Botswana Pula(P)"

a = rst["Currency"]=="Botswana Pula(P)"
print(a[0]) # may be True or False given rst[0].Currency
print(a[1]) # may be True or False given rst[1].Currency
...

Then, rst[rst["Currency"]=="Botswana Pula(P)"] returns another DataSet with just the rows that satisfies the mask. Say rst has three rows and just the first satisfies the condition:

a = rst[rst["Currency"]=="Botswana Pula(P)"]
print(a.shape) # yields (1, <whatever>)
print('Yes' if a else 'No') # yields The truth value of a DataFrame is ambiguous

Since you cannot determine the truthiness of a DataSet

That said, I humbly borrow the answer from @BenY, https://stackoverflow.com/a/67994153/206413:

def CurConv(curr):
    '''Taking a row, return a value for the conversion factor'''
    if curr == "Botswana Pula(P)":
        return 0.094
    elif curr == "Brazilian Real(R$)":
        return 0.2
    elif curr == "Dollar($)":
        return 1
    elif curr == "Emirati Diram(AED)":
        return 0.27
    elif curr == "Indian Rupees(Rs.)":
        return 0.014
    elif curr == "Indonesian Rupiah(IDR)":
        return 0.000070
    elif currency == "NewZealand($)":
        return 0.71
    elif curr == "Pounds(å£)":
        return 1.41
    elif curr == "Qatari Rial(QR)":
        return 0.27
    elif curr =="Rand(R)":
        return 0.073
    elif curr == "Sri Lankan Rupee(LKR)":
        return 0.0051
    elif curr == "Turkish Lira(TL)":
        return 0.12
    else:
        return float("nan")

rst["USD"] = rst['AverageCostfortwo'] * rst['currency'].apply(CurConv)
Related