How to add items to numpy array mapped to each dictionary key in python?

Viewed 111

I have created a dictionary as ,

dummy_dict = {'current_acc':np.empty(0,dtype='int'),
              'saving_acc':np.empty(0,dtype='int'),
              'fixed_acc':np.empty(0,dtype='int')}

I would like to fill values in numpy array value of each key in dictionary using loops,

let's, say on meeting a condition each key value should be filled in with True(1) else it should be False(0).

Below statements explains why it is requirered.

I have a custom function to create a dummy variables of given column,

def py_step_dummy(DT,col,drop_level=False):

    dummy_dict={ item : [] for item in dt.unique(DT[:,f[col]]).to_list()[0]}

    dummy_values=DT[:,f[col]].to_list()[0]

    for item in dummy_values:
        for key in dummy_dict.keys():
            if item==key:
                dummy_dict[key].append(1)
            else:
                dummy_dict[key].append(0)

    dummy_dt = dt.Frame(dummy_dict) 

   return dummy_dt 

Here I would like to replace this dictionary

dummy_dict={ item : [] for item in dt.unique(DT[:,f[col]]).to_list()[0]}

as

dummy_dict={ item : np.empty(0) for item in dt.unique(DT[:,f[col]]).to_list()[0]}

In my case dictionary with numpy arrays values would only be allowed and suggested.

could you now please help me how to do it ?.

0 Answers
Related