mardi 10 novembre 2020

Dictionaries and Variables

Say we have a dict below defined:

   dict= {'date' : np.datetime64('2020-01-05'), 
          'value' : 750}

I would like to create a new variable to be saved which is dependent on the value of dict['value'].

For instance,

df['value'] is > 500, then new_variable = 5
df['value'] is < 500, then new_variable = 1% * 5000

I tried using this new_variable as a 0 and made use of if-statements to:

new_variable = 0 OR []

if df['value'] is > 500:
    new_variable = new_variable + 5

Ideally I want a variable and not a list, I understand I probably can just append the value but that would be a hassle to use index slicing just to get that value out. Is there a way I can create the new_variable in a simple manner using if-statements such that the value will be permanently stored and is dependent on the dict['value'] value?

I am new to Python so would appreciate something simplistic and easy to comprehend, thank you so much guys !

Aucun commentaire:

Enregistrer un commentaire