Home > Mobile >  How do I iterate though a column to find one specific value in a data frame
How do I iterate though a column to find one specific value in a data frame

Time:04-07

Idea:

I am building a script that looks at google cloud instances and checks the best instance for a user.

The script gets all the google cloud instances, looks at the vCPU and suggests recommended instances if you want to downgrade.

e.g if you have a E2_performance which has 8 cVPU's and you only need 4 then you should downgrade to E2_shared-core.

The script should then give you a string to show how much money you save. E.g:

GoogleInstanceType vCPUs costYearly memory Suggested downgrade yearly saving
E2_General-purpose 2 100 1000 none 0
E2_shared-core 4 400 2000 E2_shared-core 300
E2_performance 8 1000 3000 E2_performance 600

Problem: Unfortunately, I'm not sure how to get the yearly savings column to work. I need to get the current rows costYearly and minus that by the suggested downgrade's costYearly.

E.g if I downgrade from a E2_performance which costs 1000 a year to a E2 performance which costs 400 then I will be saving 600.

Bare in mind there will be hundreds of rows. I'm just showing 3 to simplify things.

Current code:


import pandas as pd

data = {
  "GoogleInstanceType": ['E2_General-purpose', 'E2_shared-core', 'E2_performance'],
  "vCPUs": ['2', '4', '8'],
  "costYearly": ['100', '400', '1000'],
  "memory": [1000, 2000, 3000]
}

#load data into a DataFrame object:
df = pd.DataFrame(data)
df['Suggested downgrade'] = 'none'
df['Suggested downgrade'] = df['Suggested downgrade'].mask(df['GoogleInstanceType']=='E2_shared-core', other='E2_General-purpose')
df['Suggested downgrade'] = df['Suggested downgrade'].mask(df['GoogleInstanceType']=='E2_performance', other='E2_shared-core')

print(df) 


Any help would be appreciated!

CodePudding user response:

You could map Suggested downgrade to costs and subtract the corresponding costs:

df['costYearly'] = df['costYearly'].astype(float)
df['yearly saving'] = df['costYearly'] - df['Suggested downgrade'].map(df.set_index('GoogleInstanceType')['costYearly']).fillna(df['costYearly'])

Output:

   GoogleInstanceType vCPUs  costYearly  memory Suggested downgrade  yearly saving
0  E2_General-purpose     2       100.0    1000                none            0.0
1      E2_shared-core     4       400.0    2000  E2_General-purpose          300.0
2      E2_performance     8      1000.0    3000      E2_shared-core          600.0
  • Related