Alex Cahill
Alex Cahill

Reputation: 13

Apply logical comparison but truth value ambiguous

I am trying to do a mapping of current resources to available resources in gcp. I want to check the current RAM and vCPUs(calculated elsewhere) then create a new column for each machine series that fits. I have tried solutions suggested elsewhere and gotten to this point but still no luck. the error that is being thrown is:

  df[series] = df.apply(lambda x: check_machine(df, series), axis=0)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/[REDACTED]/program.py", line 220, in check_machine
    if (df['vCPUs'].lt(machine_data[series][i]["vCPUs"]))  &  (df['RAM'].gt(machine_data[series][i]["memoryGB"])):
  File "/opt/homebrew/lib/python3.11/site-packages/pandas/core/generic.py", line 1527, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

here is a sample of the data I have: | machine | RAM. | vCPUs | | --- | --- | --- | | machine1 | 16 | 8 | | machine2 | 8 | 4 |

here is an example of what I want:

machine RAM. vCPUs N1 HighMem option N2 HighMem option
machine1 16 8 n1-highmem-4 n2-highmem-4
machine2 8 4 n1-highmem-2 n2-highmem-2

the columns 'N1 HighMem option' & 'N1 HighMem option' are the rows I want to create based on the values contained in RAM and vCPUs.

here is the code I currently have:

def check_machine(df, series):

    for i in machine_data[series]:
        
        if (df['vCPUs'].lt(machine_data[series][i]["vCPUs"]))  &  (df['RAM'].gt(machine_data[series][i]["memoryGB"])):
            df[series] = machine_data[series][i]
            return(df)

        else:
            pass

def mapping_full(df):

    for series in machine_data:
        df[series] = df.apply(lambda x: check_machine(df, series), axis=0)
    return(df)

project = mapping_full(project)

I have a mapping of machine types

machine_data = {
    "n2Highmem": {
        "n2-highmem-2":{
            "vCPUs": 2,
            "memoryGB": 16
        },
    ...
}

I have used apply and map before in similar aways and thought this was the way to go until I kept hitting this "ambiguity" error.

any help is appreciated ")

previously I was using

and but changed it to &

same with

< and > changed to using the pandas .lt() and .gt() functions

Upvotes: 0

Views: 25

Answers (0)

Related Questions